Mini-Games-Game/Assets/Scripts/PlayerController.cs

148 lines
3.7 KiB
C#
Raw Normal View History

using UnityEngine;
2024-11-22 22:58:34 +00:00
using System.Collections;
using UnityEngine.UI;
#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;
[Header("Speed")]
public float speed = 5;
2024-11-22 22:58:34 +00:00
public float curSpeed = 0f;
[Header("Dash")]
public float dashSpeed = 10;
public float dashDuration = 0.3f; // Dash duration
public float dashCooldown = 2f;
public KeyCode dashKey = KeyCode.LeftShift;
public bool canDash = true;
public float dashCooldownTimer = 0f;
[Header("Lives")]
public string enemyTag = "Enemy";
public float invulnerabilityTimeSeconds = 0.25f;
public int lives = 3;
[Header("Audio")]
public AudioSource metalPipe;
[Header("Player Object")]
public Rigidbody rb;
2024-11-22 22:58:34 +00:00
private float lastHit = 0f;
void Start()
{
if (!TryGetComponent(out rb))
{
Debug.LogError("Rigidbody not found!");
enabled = false; // Disable script if Rigidbody is missing.
}
else
{
rb.freezeRotation = true;
}
}
void Update()
{
2024-11-22 22:58:34 +00:00
curSpeed = speed;
Vector3 moveDirection = GetPlayerDirection().normalized;
if (Input.GetKey(dashKey) && canDash)
{
StartCoroutine(DashCooldown()); // Start Dash Coroutine when dash is pressed
curSpeed = dashSpeed;
rb.velocity = new Vector3(
moveDirection.x * curSpeed,
rb.velocity.y,
moveDirection.z * curSpeed
);
}
else if (Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2) < speed)
{
rb.AddForce(new Vector3 (moveDirection.x * curSpeed, 0, moveDirection.z * curSpeed), ForceMode.Force);
}
}
void HandleDash()
{
}
2024-11-22 22:58:34 +00:00
IEnumerator DashCooldown()
{
2024-11-22 22:58:34 +00:00
// Start dash
canDash = false;
Vector3 dashDirection = GetPlayerDirection().normalized;
rb.velocity = dashDirection * dashSpeed; // Apply dash velocity
2024-11-22 22:58:34 +00:00
// Wait for the dash to finish
yield return new WaitForSeconds(dashDuration);
2024-11-22 22:58:34 +00:00
// End dash: reset velocity and start cooldown
rb.velocity = Vector3.zero; // Optionally reset velocity after dash (for more control)
dashCooldownTimer = dashCooldown;
// Wait for cooldown
yield return new WaitForSeconds(dashCooldown);
// Allow next dash
canDash = true;
}
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);
}
void OnCollisionEnter(Collision collision)
{
2024-11-22 22:58:34 +00:00
bool collidedWithEnemy = collision.gameObject.CompareTag(enemyTag);
bool isInvulnerable = invulnerabilityTimeSeconds >= Time.time - lastHit;
2024-11-22 22:58:34 +00:00
if (collidedWithEnemy && !isInvulnerable)
{
2024-11-22 22:58:34 +00:00
HandlePlayerHit();
}
}
2024-11-22 22:58:34 +00:00
void HandlePlayerHit()
{
lastHit = Time.time;
metalPipe.Play(); // Play damage sound effect
lives--;
// Update visual feedback (if any) for losing a life, e.g. hearts
2024-11-22 22:58:34 +00:00
if (lives <= 0)
{
ExitGame();
}
}
void ExitGame()
{
#if UNITY_EDITOR
EditorApplication.ExitPlaymode();
#endif
2024-11-22 22:58:34 +00:00
Application.Quit();
}
}