2024-04-30 17:14:05 +01:00
|
|
|
using UnityEngine;
|
2024-11-22 22:58:34 +00:00
|
|
|
using System.Collections;
|
2024-04-30 21:26:34 +01:00
|
|
|
using UnityEngine.UI;
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
using UnityEditor;
|
|
|
|
#endif
|
|
|
|
|
2024-04-30 17:14:05 +01:00
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
{
|
2024-11-24 17:12:13 +00:00
|
|
|
|
2024-04-30 17:14:05 +01:00
|
|
|
[Header("Keybinds")]
|
|
|
|
public KeyCode forwardKey = KeyCode.W;
|
|
|
|
public KeyCode backwardKey = KeyCode.S;
|
|
|
|
public KeyCode leftKey = KeyCode.A;
|
|
|
|
public KeyCode rightKey = KeyCode.D;
|
2024-11-24 17:46:56 +00:00
|
|
|
public KeyCode dashKey = KeyCode.LeftShift;
|
2024-04-30 17:14:05 +01:00
|
|
|
|
|
|
|
[Header("Speed")]
|
|
|
|
public float speed = 5;
|
2024-11-24 01:34:56 +00:00
|
|
|
public bool canMove = true;
|
2024-11-22 22:58:34 +00:00
|
|
|
|
|
|
|
[Header("Dash")]
|
|
|
|
public float dashSpeed = 10;
|
|
|
|
public float dashDuration = 0.3f; // Dash duration
|
|
|
|
public float dashCooldown = 2f;
|
|
|
|
public bool canDash = true;
|
2024-11-24 17:46:56 +00:00
|
|
|
public ParticleSystem dashParticles;
|
|
|
|
public Transform dashParticlesTransform;
|
2024-11-24 01:34:56 +00:00
|
|
|
|
|
|
|
|
2024-04-30 21:26:34 +01:00
|
|
|
[Header("Lives")]
|
|
|
|
public string enemyTag = "Enemy";
|
2024-11-24 17:46:56 +00:00
|
|
|
public float invulnerabilityTime = 0.25f;
|
|
|
|
public bool isInvulnerable = false;
|
2024-04-30 21:26:34 +01:00
|
|
|
public int lives = 3;
|
2024-11-24 17:46:56 +00:00
|
|
|
public ParticleSystem bloodParticles;
|
|
|
|
public AudioSource damageSound;
|
2024-04-30 17:31:29 +01:00
|
|
|
|
2024-04-30 21:26:34 +01:00
|
|
|
[Header("Player Object")]
|
|
|
|
public Rigidbody rb;
|
2024-11-24 17:46:56 +00:00
|
|
|
public BoxCollider collision;
|
2024-04-30 21:26:34 +01:00
|
|
|
|
2024-11-24 17:46:56 +00:00
|
|
|
|
2024-11-22 22:58:34 +00:00
|
|
|
|
2024-04-30 17:14:05 +01:00
|
|
|
void Start()
|
|
|
|
{
|
2024-04-30 21:26:34 +01:00
|
|
|
if (!TryGetComponent(out rb))
|
|
|
|
{
|
|
|
|
Debug.LogError("Rigidbody not found!");
|
|
|
|
enabled = false; // Disable script if Rigidbody is missing.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb.freezeRotation = true;
|
|
|
|
}
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
2024-11-22 22:58:34 +00:00
|
|
|
Vector3 moveDirection = GetPlayerDirection().normalized;
|
|
|
|
|
|
|
|
if (Input.GetKey(dashKey) && canDash)
|
|
|
|
{
|
2024-11-24 01:34:56 +00:00
|
|
|
StartCoroutine(Dash());
|
|
|
|
}
|
|
|
|
else if (canMove)
|
|
|
|
{
|
2024-11-22 22:58:34 +00:00
|
|
|
rb.velocity = new Vector3(
|
2024-11-24 17:46:56 +00:00
|
|
|
moveDirection.x * speed,
|
2024-11-22 22:58:34 +00:00
|
|
|
rb.velocity.y,
|
2024-11-24 17:46:56 +00:00
|
|
|
moveDirection.z * speed
|
2024-11-22 22:58:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-24 01:34:56 +00:00
|
|
|
IEnumerator Dash()
|
2024-11-22 22:58:34 +00:00
|
|
|
{
|
2024-11-24 01:34:56 +00:00
|
|
|
canMove = false;
|
2024-11-22 22:58:34 +00:00
|
|
|
canDash = false;
|
|
|
|
Vector3 dashDirection = GetPlayerDirection().normalized;
|
2024-11-24 01:34:56 +00:00
|
|
|
rb.velocity = dashDirection * dashSpeed;
|
|
|
|
|
2024-11-24 17:46:56 +00:00
|
|
|
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);
|
2024-04-30 21:26:34 +01:00
|
|
|
|
2024-11-22 22:58:34 +00:00
|
|
|
yield return new WaitForSeconds(dashDuration);
|
2024-04-30 21:26:34 +01:00
|
|
|
|
2024-11-24 17:46:56 +00:00
|
|
|
dashParticles.Stop();
|
2024-11-24 01:34:56 +00:00
|
|
|
|
|
|
|
rb.velocity = Vector3.zero;
|
|
|
|
canMove = true;
|
2024-11-22 22:58:34 +00:00
|
|
|
|
|
|
|
yield return new WaitForSeconds(dashCooldown);
|
|
|
|
|
|
|
|
canDash = true;
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|
|
|
|
|
2024-11-22 22:58:34 +00:00
|
|
|
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);
|
|
|
|
}
|
2024-04-30 21:26:34 +01:00
|
|
|
|
2024-04-30 17:14:05 +01:00
|
|
|
void OnCollisionEnter(Collision collision)
|
|
|
|
{
|
2024-11-22 22:58:34 +00:00
|
|
|
bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag);
|
|
|
|
|
2024-04-30 21:26:34 +01:00
|
|
|
if (collidedWithEnemy && !isInvulnerable)
|
2024-04-30 17:14:05 +01:00
|
|
|
{
|
2024-11-24 17:46:56 +00:00
|
|
|
StartCoroutine(Hit());
|
2024-11-22 22:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-30 21:26:34 +01:00
|
|
|
|
2024-11-24 17:46:56 +00:00
|
|
|
IEnumerator Hit()
|
2024-11-22 22:58:34 +00:00
|
|
|
{
|
|
|
|
lives--;
|
|
|
|
if (lives <= 0)
|
|
|
|
{
|
|
|
|
ExitGame();
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|
2024-11-24 17:12:13 +00:00
|
|
|
|
2024-11-24 17:46:56 +00:00
|
|
|
bloodParticles.Play();
|
|
|
|
|
|
|
|
damageSound.Play();
|
|
|
|
|
|
|
|
rb.isKinematic = true;
|
|
|
|
isInvulnerable = true;
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(invulnerabilityTime / 2);
|
|
|
|
|
|
|
|
rb.isKinematic = false;
|
2024-11-24 17:12:13 +00:00
|
|
|
|
2024-11-24 17:46:56 +00:00
|
|
|
yield return new WaitForSeconds(invulnerabilityTime / 2);
|
2024-11-24 17:12:13 +00:00
|
|
|
|
2024-11-24 17:46:56 +00:00
|
|
|
isInvulnerable = false;
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|
2024-04-30 21:26:34 +01:00
|
|
|
|
|
|
|
void ExitGame()
|
|
|
|
{
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
EditorApplication.ExitPlaymode();
|
|
|
|
#endif
|
2024-11-22 22:58:34 +00:00
|
|
|
Application.Quit();
|
2024-04-30 21:26:34 +01:00
|
|
|
}
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|