119 lines
3.5 KiB
C#
119 lines
3.5 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
private float speed = 25f;
|
|
private float moveInput;
|
|
private float turnInput;
|
|
private float boostInput;
|
|
bool isBoosting = false;
|
|
float boostTimer = 0f;
|
|
float boostMultiplier = 1.5f;
|
|
public float boostDuration = 1f;
|
|
float cooldownTimer = 0f;
|
|
public float boostCooldown = 5f;
|
|
private Rigidbody rb;
|
|
public bool gameOver = false;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
moveInput = Input.GetAxisRaw("Vertical");
|
|
turnInput = Input.GetAxisRaw("Horizontal");
|
|
if (moveInput > 0 && Input.GetKeyDown(KeyCode.Space) && !isBoosting && cooldownTimer <= 0f && !gameOver)
|
|
{
|
|
isBoosting = true;
|
|
boostTimer = boostDuration;
|
|
}
|
|
|
|
// Cooldown
|
|
if (cooldownTimer > 0f)
|
|
cooldownTimer -= Time.deltaTime;
|
|
|
|
if (transform.position.x < -22f)
|
|
{
|
|
// Prevent going too low
|
|
transform.position = new Vector3(-22f, transform.position.y, transform.position.z);
|
|
rb.linearVelocity = Vector3.zero;
|
|
}
|
|
else if (transform.position.x > 22f)
|
|
{
|
|
// Prevent going too high
|
|
transform.position = new Vector3(22f, transform.position.y, transform.position.z);
|
|
rb.linearVelocity = Vector3.zero;
|
|
}
|
|
if (transform.position.z < -40f)
|
|
{
|
|
// Prevent going too low
|
|
transform.position = new Vector3(transform.position.x, transform.position.y, -40f);
|
|
rb.linearVelocity = Vector3.zero;
|
|
}
|
|
else if (transform.position.z > 40f)
|
|
{
|
|
// Prevent going too high
|
|
transform.position = new Vector3(transform.position.x, transform.position.y, 40f);
|
|
rb.linearVelocity = Vector3.zero;
|
|
}
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
float currentSpeed = speed;
|
|
|
|
// ----- Boost Timer -----
|
|
if (isBoosting)
|
|
{
|
|
boostTimer -= Time.fixedDeltaTime;
|
|
currentSpeed = speed * boostMultiplier;
|
|
|
|
if (boostTimer <= 0f)
|
|
{
|
|
isBoosting = false;
|
|
cooldownTimer = boostCooldown; // Cooldown starten
|
|
}
|
|
}
|
|
|
|
// ----- Movement -----
|
|
if (!gameOver)
|
|
{
|
|
Vector3 move = transform.right * moveInput * currentSpeed * Time.fixedDeltaTime;
|
|
rb.MovePosition(rb.position + move);
|
|
Vector3 turn = transform.forward * -turnInput * currentSpeed * Time.fixedDeltaTime;
|
|
rb.MovePosition(rb.position + turn);
|
|
}
|
|
}
|
|
|
|
void OnCollisionEnter(Collision collision)
|
|
{
|
|
// Prüfen, ob der Spieler mit einem Enemy oder Hindernis kollidiert
|
|
if (collision.gameObject.CompareTag("Enemy") || collision.gameObject.CompareTag("Obstacle"))
|
|
{
|
|
gameOver = true;
|
|
Debug.Log("Game Over!");
|
|
}
|
|
}
|
|
|
|
void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.GetComponent<SafezoneData>() != null)
|
|
{
|
|
other.GetComponent<SafezoneData>().playerOn = true;
|
|
other.GetComponent<SafezoneData>().playerHasBeenOn = true;
|
|
}
|
|
}
|
|
|
|
void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.GetComponent<SafezoneData>() != null)
|
|
{
|
|
other.GetComponent<SafezoneData>().playerOn = false;
|
|
}
|
|
}
|
|
}
|