2024-04-30 17:14:05 +01:00
|
|
|
using UnityEngine;
|
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
|
|
|
|
{
|
|
|
|
[Header("Keybinds")]
|
|
|
|
public KeyCode forwardKey = KeyCode.W;
|
|
|
|
public KeyCode backwardKey = KeyCode.S;
|
|
|
|
public KeyCode leftKey = KeyCode.A;
|
|
|
|
public KeyCode rightKey = KeyCode.D;
|
|
|
|
|
|
|
|
[Header("Speed")]
|
|
|
|
public float speed = 5;
|
|
|
|
|
2024-04-30 21:26:34 +01:00
|
|
|
[Header("Lives")]
|
|
|
|
public string enemyTag = "Enemy";
|
|
|
|
public float invulnerabilityTimeSeconds = 0.25f;
|
|
|
|
public float lastHit = 0;
|
|
|
|
public int lives = 3;
|
|
|
|
public GameObject[] hearts;
|
2024-04-30 17:14:05 +01:00
|
|
|
|
2024-04-30 17:31:29 +01:00
|
|
|
[Header("Audio")]
|
|
|
|
public AudioSource metalPipe;
|
|
|
|
|
2024-04-30 21:26:34 +01:00
|
|
|
[Header("Player Object")]
|
|
|
|
public Vector3 moveDirection;
|
|
|
|
public Rigidbody rb;
|
|
|
|
|
2024-04-30 17:14:05 +01:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2024-04-30 21:26:34 +01:00
|
|
|
if (hearts.Length != 3)
|
|
|
|
{
|
|
|
|
Debug.LogError("Hearts array not properly configured. Expected length 3.");
|
|
|
|
enabled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
moveDirection = PlayerInput();
|
|
|
|
rb.velocity = moveDirection.normalized * speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 PlayerInput()
|
|
|
|
{
|
2024-04-30 21:26:34 +01:00
|
|
|
Vector3 direction = Vector3.zero;
|
|
|
|
|
|
|
|
if (Input.GetKey(rightKey)) direction.x += 1f;
|
|
|
|
if (Input.GetKey(leftKey)) direction.x -= 1f;
|
|
|
|
if (Input.GetKey(forwardKey)) direction.z += 1f;
|
|
|
|
if (Input.GetKey(backwardKey)) direction.z -= 1f;
|
|
|
|
|
|
|
|
return direction;
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|
|
|
|
|
2024-04-30 21:26:34 +01:00
|
|
|
|
2024-04-30 17:14:05 +01:00
|
|
|
void OnCollisionEnter(Collision collision)
|
|
|
|
{
|
2024-04-30 21:26:34 +01:00
|
|
|
bool collidedWithEnemy = collision.gameObject.tag == enemyTag;
|
|
|
|
bool isInvulnerable = invulnerabilityTimeSeconds >= Time.time - lastHit;
|
|
|
|
if (collidedWithEnemy && !isInvulnerable)
|
2024-04-30 17:14:05 +01:00
|
|
|
{
|
2024-04-30 21:26:34 +01:00
|
|
|
lastHit = Time.time;
|
2024-04-30 17:31:29 +01:00
|
|
|
metalPipe.Play();
|
2024-04-30 21:26:34 +01:00
|
|
|
|
|
|
|
lives--;
|
|
|
|
hearts[lives].GetComponent<RawImage>().color = Color.gray;
|
|
|
|
|
|
|
|
bool playerOutOfLives = lives == 0;
|
|
|
|
if (playerOutOfLives) ExitGame();
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-30 21:26:34 +01:00
|
|
|
|
|
|
|
void ExitGame()
|
|
|
|
{
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
EditorApplication.ExitPlaymode();
|
|
|
|
#endif
|
|
|
|
Application.Quit();
|
|
|
|
}
|
2024-04-30 17:14:05 +01:00
|
|
|
}
|