4
0
This repository has been archived on 2023-09-23. You can view files and clone it, but cannot push or open issues or pull requests.
TremendousTaxi/game/Assets/Scripts/CarController.cs

171 lines
3.6 KiB
C#
Raw Permalink Normal View History

2022-09-24 14:21:41 +02:00
using System.Xml.Schema;
using System;
2022-09-25 10:29:51 +02:00
using System.Threading;
2022-09-24 14:21:41 +02:00
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2022-09-25 10:29:51 +02:00
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;
2022-09-24 14:21:41 +02:00
2022-09-24 15:59:56 +02:00
public class CarController : MonoBehaviour
2022-09-24 14:21:41 +02:00
{
public float speed {get; set;}
2022-09-24 14:21:41 +02:00
private float rotation;
public float MaxLeftSteer = -45;
public float MaxRightSteer = 45;
public float MaxForwardSpeed = 1024;
public float MaxBackwardSpeed = -16;
public float EngineBreakPower = 2f;
public float Acceleration = 1;
public float BreakForce = 10;
public float SteerStraighteningPower = 2f;
public float AbruptBreak =2;
2022-09-24 15:59:56 +02:00
//Variablen gegen Saltos
public LayerMask layerMask;
public Transform groundCheck;
//Variablen für explosionen
2022-09-25 10:29:51 +02:00
public GameObject boom;
public Rigidbody car;
public Transform spawnPoint;
private bool playerExplosion = false;
private bool exploding = false;
2022-09-24 15:59:56 +02:00
2022-09-24 14:21:41 +02:00
// Start is called before the first frame update
void Start()
{
speed = 0;
rotation = 0;
}
2022-09-25 10:29:51 +02:00
void OnCollisionEnter(Collision collision)
{
if (collision.relativeVelocity.magnitude > 5)
exploding = true;
}
2022-09-24 14:21:41 +02:00
// Update is called once per frame
2022-09-24 15:59:56 +02:00
void FixedUpdate()
{
if (OnGround())
{
Drive();
2022-09-25 10:29:51 +02:00
}
if ((Input.GetAxis("Fire1") != 0 && playerExplosion == false) || (exploding && playerExplosion == false)) {
StartCoroutine(Explode());
2022-09-24 15:59:56 +02:00
}
}
2022-09-25 10:29:51 +02:00
IEnumerator Explode()
{
playerExplosion = true;
GameObject currentBoom = Instantiate(boom, transform.position, Quaternion.identity);
car.velocity.Set(0,0,0);
yield return new WaitForSeconds(0.5f);
exploding = false;
transform.position = spawnPoint.position;
transform.rotation = spawnPoint.rotation;
speed = 0;
playerExplosion = false;
Destroy(currentBoom);
}
2022-09-24 15:59:56 +02:00
bool OnGround()
{
return Physics.CheckSphere(groundCheck.position, 1f, layerMask);
}
void Drive()
2022-09-24 14:21:41 +02:00
{
float InRot = Input.GetAxis("Horizontal");
float InSp = Input.GetAxis("Vertical");
// Steering
if(InRot==0)
{
if(speed!=0)
{
rotation -= rotation*SteerStraighteningPower*Time.deltaTime;
}
}
else
{
if((rotation>0 && InRot<0)||(rotation<0 && InRot>0))
rotation=0;
rotation += InRot;
}
rotation -= InRot*Time.deltaTime;
if(Input.GetAxis("Jump")>0) //Breaking
{
if(speed>0)
{
speed -= BreakForce*Time.deltaTime;
speed = Mathf.Clamp(speed, 0, MaxForwardSpeed);
}
else
{
speed += BreakForce*Time.deltaTime;
speed = Mathf.Clamp(speed, MaxBackwardSpeed, 0);
}
if(Mathf.Abs(speed)<AbruptBreak)
speed = 0;
}
else if(InSp == 0) //Engine Breaking
{
if(speed>0)
{
speed -= speed*EngineBreakPower*Time.deltaTime;
speed = Mathf.Clamp(speed, 0, MaxForwardSpeed);
}
else
{
speed -= speed*EngineBreakPower*Time.deltaTime;
speed = Mathf.Clamp(speed, MaxBackwardSpeed, 0);
}
}
else if(InSp>0) // Acceleration
{
speed += Acceleration;
}
else if(InSp<0)
{
speed -= Acceleration;
}
if(Mathf.Abs(speed)<0.1f) // Abrupt break
speed=0;
if(Mathf.Abs(rotation)<0.1f) // Abrupt straightening
rotation=0;
// Max values
rotation = Mathf.Clamp(rotation, MaxLeftSteer, MaxRightSteer);
speed = Mathf.Clamp(speed, MaxBackwardSpeed, MaxForwardSpeed);
if(speed>0) //Moving and rotating the car
{
transform.Translate(UnityEngine.Vector3.forward * Mathf.Sqrt(speed) * Time.deltaTime);
transform.Rotate(0f,rotation*Time.deltaTime,0f);
}
else if (speed<0)
{
transform.Translate(UnityEngine.Vector3.back * Mathf.Sqrt(-speed) * Time.deltaTime);
transform.Rotate(0f,-rotation*Time.deltaTime,0f);
}
}
}