148 lines
4.5 KiB
C#
148 lines
4.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
public float motorTorque = 2000;
|
|
public float brakeTorque = 2000;
|
|
public float maxSpeed = 20;
|
|
public float steeringRange = 30;
|
|
public float steeringRangeAtMaxSpeed = 10;
|
|
public float autoBrake;
|
|
public AudioSource audio;
|
|
WheelControl[] wheels;
|
|
public Rigidbody rigidBody;
|
|
[HideInInspector] public int checkpointsCollected = 0;
|
|
public GameObject[] checkpoints;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
if (rigidBody == null)
|
|
{
|
|
rigidBody = GetComponent<Rigidbody> ();
|
|
}
|
|
|
|
|
|
// Find all child GameObjects that have the WheelControl script attached
|
|
wheels = GetComponentsInChildren<WheelControl>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
|
|
float vInput = Input.GetAxis("Vertical");
|
|
float hInput = Input.GetAxis("Horizontal");
|
|
|
|
// Calculate current speed in relation to the forward direction of the car
|
|
// (this returns a negative number when traveling backwards)
|
|
float forwardSpeed = Vector3.Dot(transform.forward, rigidBody.velocity);
|
|
|
|
|
|
// Calculate how close the car is to top speed
|
|
// as a number from zero to one
|
|
float speedFactor = Mathf.InverseLerp(0, maxSpeed / 4, forwardSpeed);
|
|
|
|
// Use that to calculate how much torque is available
|
|
// (zero torque at top speed)
|
|
float currentMotorTorque = Mathf.Lerp(motorTorque, 0, speedFactor);
|
|
|
|
// …and to calculate how much to steer
|
|
// (the car steers more gently at top speed)
|
|
|
|
float currentSteerRange = Mathf.Lerp(steeringRange, steeringRangeAtMaxSpeed, speedFactor);
|
|
|
|
// Check whether the user input is in the same direction
|
|
// as the car's velocity
|
|
bool isAccelerating = Mathf.Sign(vInput) == Mathf.Sign(forwardSpeed);
|
|
|
|
bool isStopping = vInput == 0; // range
|
|
|
|
bool isBraking = (vInput < 0 && forwardSpeed > 0) || (vInput > 0 && forwardSpeed < 0);
|
|
|
|
if (vInput > 0 && forwardSpeed < 0)
|
|
{
|
|
isAccelerating = false;
|
|
}
|
|
|
|
foreach (var wheel in wheels)
|
|
{
|
|
// Apply steering to Wheel colliders that have "Steerable" enabled
|
|
if (wheel.steerable)
|
|
{
|
|
wheel.WheelCollider.steerAngle = hInput * currentSteerRange;
|
|
}
|
|
|
|
if (isBraking)
|
|
{
|
|
wheel.WheelCollider.brakeTorque = Mathf.Abs(vInput) * brakeTorque;
|
|
//wheel.WheelCollider.motorTorque = 0;
|
|
}
|
|
|
|
if (isAccelerating)
|
|
{
|
|
// Apply torque to Wheel colliders that have "Motorized" enabled
|
|
if (wheel.motorized)
|
|
{
|
|
wheel.WheelCollider.motorTorque = vInput * currentMotorTorque;
|
|
}
|
|
wheel.WheelCollider.brakeTorque = 0;
|
|
}
|
|
|
|
|
|
|
|
if (isStopping)
|
|
{
|
|
// If the user is trying to go in the opposite direction
|
|
// apply brakes to all wheels
|
|
wheel.WheelCollider.brakeTorque = Mathf.Abs(vInput) * brakeTorque + autoBrake;
|
|
|
|
if (forwardSpeed < 0)
|
|
{
|
|
wheel.WheelCollider.brakeTorque = (Mathf.Abs(vInput) * brakeTorque + autoBrake) * 5;
|
|
}
|
|
|
|
// wheel.WheelCollider.motorTorque = 0;
|
|
}
|
|
}
|
|
|
|
//idk camera mobning rammer hårdt
|
|
// float THINP = hInput/10;
|
|
// if (THINP != hInput)
|
|
// {
|
|
// THINP+=hInput/10;
|
|
// }
|
|
|
|
|
|
//lookat.transform.localPosition = new Vector3 (THINP*1.5f,1,4);
|
|
|
|
|
|
}
|
|
|
|
private void Update() {
|
|
// Transform currentCheckpoint = checkpoints[checkpointsCollected].transform;
|
|
// float checkpintDistance = distanceToCheckpoint(currentCheckpoint);
|
|
|
|
// if (checkpintDistance < 0.1f)
|
|
// {
|
|
// checkpointsCollected += 1;
|
|
// }
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision other) {
|
|
if (other.gameObject.tag == "Wall")
|
|
{
|
|
// audio.Play();
|
|
}
|
|
}
|
|
|
|
float distanceToCheckpoint(Transform checkpoint)
|
|
{
|
|
var closestPoint = checkpoint.GetComponent<Collider>().ClosestPointOnBounds(transform.position);
|
|
var distanceToCheckpoint = Vector3.Distance(transform.position, closestPoint);
|
|
return distanceToCheckpoint;
|
|
}
|
|
}
|