Qu/Assets/Scripts/PlayerController.cs
Gerard Gascón bd5b1556ff init
2025-04-24 14:23:29 +02:00

135 lines
3.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour{
[HideInInspector]
public Vector2 lastCheckpoint;
public SpriteRenderer[] deathDisable;
public float speed;
public float airSpeed;
[HideInInspector]
public bool grappled;
[HideInInspector]
public bool ableToGrapple;
[Header("Jump")]
public float jumpForce;
public float groundDetectionRadius;
public LayerMask whatIsGround;
[Space(10)]
public GameObject death;
public Transform gun;
public Transform firePos;
LineRenderer line;
bool dead;
bool moving;
bool jump;
bool isGrounded;
Rigidbody2D rb2d;
Shoot shoot;
// Start is called before the first frame update
void Start(){
AudioManager.instance.Play("Theme");
shoot = GetComponent<Shoot>();
rb2d = GetComponent<Rigidbody2D>();
line = GetComponent<LineRenderer>();
lastCheckpoint = transform.position;
}
// Update is called once per frame
void Update(){
if (!dead){
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
gun.transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
isGrounded = Physics2D.OverlapCircle(transform.position, groundDetectionRadius, whatIsGround);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
jump = true;
}
}
void LateUpdate(){
line.SetPosition(0, firePos.position);
}
private void OnDrawGizmosSelected(){
Gizmos.DrawWireSphere(transform.position, groundDetectionRadius);
}
void FixedUpdate(){
if (!dead){
if (!moving && isGrounded){
float fixedVelocity = rb2d.angularVelocity;
fixedVelocity *= .98f;
rb2d.angularVelocity = fixedVelocity;
}
float h = Input.GetAxisRaw("Horizontal");
if (h != 0){
moving = true;
}else{
moving = false;
}
if (isGrounded && !grappled){
rb2d.AddForce(Vector2.right * speed * h);
float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -speed, speed);
rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
}else if (!grappled){
rb2d.AddForce(Vector2.right * airSpeed * h);
float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -airSpeed, airSpeed);
rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
}
if (Input.GetMouseButton(0) && !ableToGrapple){
shoot.Fire();
}
if (jump){
rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
rb2d.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
jump = false;
}
}
}
void OnParticleCollision(GameObject col){
Instantiate(death, transform.position, Quaternion.identity);
Die();
}
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.tag == "Death"){
Instantiate(death, transform.position, Quaternion.identity);
Die();
}
}
void Die(){
foreach(SpriteRenderer s in deathDisable){
s.enabled = false;
}
GetComponent<CapsuleCollider2D>().isTrigger = true;
Invoke("Respawn", 2f);
dead = true;
rb2d.bodyType = RigidbodyType2D.Static;
}
void Respawn(){
transform.position = lastCheckpoint;
foreach (SpriteRenderer s in deathDisable){
s.enabled = true;
}
GetComponent<CapsuleCollider2D>().isTrigger = false;
rb2d.bodyType = RigidbodyType2D.Dynamic;
rb2d.velocity = Vector2.zero;
dead = false;
}
}