Code Tokyo Glow

...
The car is build in Unity as a prefab. On TheCar itself there are a couple scripts. For the input of the car, a boost manager and some more. Also there are the rigidbody and the box collider for the entire car.

TheCar has 2 child objects. One of them is the body and that contains the model of the car and all camera angles. The other one, Wheels, also has 2 childs. The first one for the colliders, which I will go deeper into in a second. And the last one is for the models of the wheels.
...
Each wheel has 3 components. First we have the transform, which U can decide the position, rotation and scale of the collider of the wheel with.

The second component is the wheel collider. It's a Unity component which u can put on your wheel and can tweek a lot. This is one of the most time comsuming thing about the car, because it has so many settings. One setting can change to car from having oversteer all the time to having understeer all the time.

And last but not least, the Wheel script. By turning on/off the checkmarks u can decide if you want that wheel to be able to turn and if it can give power. That way u can decide if the car has front or rear wheel drive.
Results:

The Full Scripts
|
v


                            
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerCarInput : MonoBehaviour
{
    public float Power;
    public float Angle;

    public CarInput input;
    public Wheel[] wheels;

    private float throttleInput;
    private float throttleDamp;

    private float steeringInput;
    private float steeringDamp;

    public float dampenSpeed;
    private KmhCounter kmhCounter;

    [SerializeField] private bool IsPlayer2;

    private void Awake()
    {
        kmhCounter = FindObjectOfType<KmhCounter>();
        input = new CarInput();
    }
    private void OnEnable()
    {
        input.Enable();
        if (!IsPlayer2)
        {
            input.Car.Throttle.performed += ApplyThrottle;
            input.Car.Throttle.canceled += ReleaseThrottle;
            input.Car.Steering.performed += ApplySteering;
            input.Car.Steering.canceled += ReleaseSteering;
        }
        else
        {
            input.Car.ThrottleP2.performed += ApplyThrottle;
            input.Car.ThrottleP2.canceled += ReleaseThrottle;
            input.Car.SteeringP2.performed += ApplySteering;
            input.Car.SteeringP2.canceled += ReleaseSteering;
        }
        
    }
    private void Update()
    {
        AngleBasedOnSpeed();
        throttleDamp = DampenedInput(throttleInput, throttleDamp);
        steeringDamp = DampenedInput(steeringInput, steeringDamp);
    }
    private void OnDisable()
    {
        input.Disable();
    }
    private float DampenedInput(float input, float output)
    {
        return Mathf.Lerp(output, input, Time.deltaTime * dampenSpeed);
    }
    private void ApplyThrottle(InputAction.CallbackContext value)
    {
        throttleInput = value.ReadValue<float>();
    }
    private void ReleaseThrottle(InputAction.CallbackContext value)
    {
        throttleInput = 0;
    }

    private void ApplySteering(InputAction.CallbackContext value)
    {
        steeringInput = value.ReadValue<float>();
    }
    private void ReleaseSteering(InputAction.CallbackContext value)
    {
        steeringInput = 0;
    }

    private void FixedUpdate()
    {
        foreach (Wheel wheel in wheels)
        {
            wheel.Accelerate(throttleDamp * Power);
            wheel.Turn(steeringDamp * Angle);
        }
    }

    private void AngleBasedOnSpeed()
    {
        Angle = 30 - kmhCounter.speed * 0.1f;
    }
}
                            
                        
                            
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wheel : MonoBehaviour
{
    public bool isPowered;
    public bool canTurn;
    public GameObject Visual;

    public float steeringAngle;

    private WheelCollider wheelCollider;

    private void Start()
    {
        wheelCollider = GetComponent<WheelCollider>();
    }

    private void Update()
    {
        Vector3 position;
        Quaternion rotation;
        wheelCollider.GetWorldPose(out position, out rotation);
        Visual.transform.position = position;
    }

    public void Accelerate(float power)
    {
        if (isPowered)
        {
            wheelCollider.motorTorque = power;
        }
    }

    public void Turn(float angle)
    {
        if (canTurn)
        {
            wheelCollider.steerAngle = angle;
        }
    }

    public void Brake(float power)
    {
        wheelCollider.brakeTorque = power;
    }
}
                            
                        

Go back

Tokyo Glow

or

Home Page