159 lines
3.8 KiB
C#
159 lines
3.8 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
|
|
[Header("Keybinds")]
|
|
public KeyCode forwardKey = KeyCode.W;
|
|
public KeyCode backwardKey = KeyCode.S;
|
|
public KeyCode leftKey = KeyCode.A;
|
|
public KeyCode rightKey = KeyCode.D;
|
|
public KeyCode dashKey = KeyCode.LeftShift;
|
|
|
|
[Header("Speed")]
|
|
public float speed = 5;
|
|
public bool canMove = true;
|
|
|
|
[Header("Dash")]
|
|
public float dashSpeed = 10;
|
|
public float dashDuration = 0.3f; // Dash duration
|
|
public float dashCooldown = 2f;
|
|
public bool canDash = true;
|
|
public ParticleSystem dashParticles;
|
|
public Transform dashParticlesTransform;
|
|
|
|
|
|
[Header("Lives")]
|
|
public string enemyTag = "Enemy";
|
|
public float invulnerabilityTime = 0.25f;
|
|
public bool isInvulnerable = false;
|
|
public int lives = 3;
|
|
public ParticleSystem bloodParticles;
|
|
public AudioSource damageSound;
|
|
public TMP_Text livesUI;
|
|
|
|
[Header("Player Object")]
|
|
public Rigidbody rb;
|
|
public BoxCollider collision;
|
|
|
|
|
|
|
|
void Start()
|
|
{
|
|
if (!TryGetComponent(out rb))
|
|
{
|
|
Debug.LogError("Rigidbody not found!");
|
|
enabled = false; // Disable script if Rigidbody is missing.
|
|
}
|
|
else
|
|
{
|
|
rb.freezeRotation = true;
|
|
livesUI.text = lives.ToString();
|
|
}
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
Vector3 moveDirection = GetPlayerDirection().normalized;
|
|
|
|
if (Input.GetKey(dashKey) && canDash)
|
|
{
|
|
StartCoroutine(Dash());
|
|
}
|
|
else if (canMove)
|
|
{
|
|
rb.velocity = new Vector3(
|
|
moveDirection.x * speed,
|
|
rb.velocity.y,
|
|
moveDirection.z * speed
|
|
);
|
|
}
|
|
}
|
|
|
|
IEnumerator Dash()
|
|
{
|
|
canMove = false;
|
|
canDash = false;
|
|
Vector3 dashDirection = GetPlayerDirection().normalized;
|
|
rb.velocity = dashDirection * dashSpeed;
|
|
|
|
dashParticles.Play();
|
|
// Positions the dash particle system behind the player so that they
|
|
// follow the player in a nice way
|
|
dashParticlesTransform.SetLocalPositionAndRotation(dashDirection * -1.5f, dashParticlesTransform.localRotation);
|
|
|
|
yield return new WaitForSeconds(dashDuration);
|
|
|
|
dashParticles.Stop();
|
|
|
|
rb.velocity = Vector3.zero;
|
|
canMove = true;
|
|
|
|
yield return new WaitForSeconds(dashCooldown);
|
|
|
|
canDash = true;
|
|
}
|
|
|
|
Vector3 GetPlayerDirection()
|
|
{
|
|
float horizontal = 0f;
|
|
float vertical = 0f;
|
|
|
|
if (Input.GetKey(rightKey)) horizontal += 1f;
|
|
if (Input.GetKey(leftKey)) horizontal -= 1f;
|
|
if (Input.GetKey(forwardKey)) vertical += 1f;
|
|
if (Input.GetKey(backwardKey)) vertical -= 1f;
|
|
|
|
return new Vector3(horizontal, 0, vertical);
|
|
}
|
|
|
|
void OnCollisionEnter(Collision collision)
|
|
{
|
|
bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag);
|
|
|
|
if (collidedWithEnemy && !isInvulnerable)
|
|
{
|
|
StartCoroutine(Hit());
|
|
}
|
|
}
|
|
|
|
IEnumerator Hit()
|
|
{
|
|
lives--;
|
|
livesUI.text = lives.ToString();
|
|
if (lives <= 0)
|
|
{
|
|
ExitGame();
|
|
}
|
|
|
|
bloodParticles.Play();
|
|
|
|
damageSound.Play();
|
|
|
|
rb.isKinematic = true;
|
|
isInvulnerable = true;
|
|
|
|
yield return new WaitForSeconds(invulnerabilityTime / 2);
|
|
|
|
rb.isKinematic = false;
|
|
|
|
yield return new WaitForSeconds(invulnerabilityTime / 2);
|
|
|
|
isInvulnerable = false;
|
|
}
|
|
|
|
void ExitGame()
|
|
{
|
|
#if UNITY_EDITOR
|
|
EditorApplication.ExitPlaymode();
|
|
#endif
|
|
Application.Quit();
|
|
}
|
|
}
|