Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Car Controller Script

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 2
    • 0
    • 42.1k
    Comment on it

    Here is the car controller script, just add this script to the car object and for values you can view the attached screenshots.

    This script contains the normal code for car controller, In this code the car object is just simply moving according to the given inputs which is just a simple stuff for anyone in unity. the major thing was wheel rotation. Now there are two parts which wheels are following.

    1. Wheels are rotating around its axis.

    2. The wheels are changing their angle as they are getting direction. 

    Now when wheels angles are changing the car object is able to move in a desired direction. Now we have put full efforts to the values which are applied in the inspector of car object. Just add this script to your car object and you can also change the values according to your own satisfaction.

    using UnityEngine;
    using System.Collections;
    
    public class CarController : MonoBehaviour {
    
        public WheelCollider WheelFL;
        public WheelCollider WheelFR;
        public WheelCollider WheelRL;
        public WheelCollider WheelRR;
    
        public Transform WheelFLtrans;
        public Transform WheelFRtrans;
        public Transform WheelRLtrans;
        public Transform WheelRRtrans;
        public Vector3 eulertest;
        float maxFwdSpeed = -3000;
        float maxBwdSpeed = 1000f;
        float gravity = 9.8f;
    
        private bool braked = false;
        private float maxBrakeTorque = 500;
    
        private Rigidbody rb;
        public Transform centreofmass;
    
        private float maxTorque = 1000;
    
        void Start () 
        {
            rb = GetComponent<Rigidbody>();
            rb.centerOfMass = centreofmass.transform.localPosition;
        }
        
       void FixedUpdate () {
    
         if(!braked){
                WheelFL.brakeTorque = 0;
                WheelFR.brakeTorque = 0;
                WheelRL.brakeTorque = 0;
                WheelRR.brakeTorque = 0;
            }
    
            //speed of car, Car will move as you will provide the input to it.
       
    
          WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
            WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
    
          
            //changing car direction
    Here we are changing the steer angle of the front tyres of the car so that we can change the car direction.
    
            WheelFL.steerAngle = 30 * (Input.GetAxis("Horizontal"));
            WheelFR.steerAngle = 30 * Input.GetAxis("Horizontal");
        }
    
        void Update()
        {
            HandBrake();
            
            //for tyre rotate
    
            WheelFLtrans.Rotate(WheelFL.rpm/60*360*Time.deltaTime ,0,0);
            WheelFRtrans.Rotate(WheelFR.rpm/60*360*Time.deltaTime ,0,0);
            WheelRLtrans.Rotate(WheelRL.rpm/60*360*Time.deltaTime ,0,0);
            WheelRRtrans.Rotate(WheelRL.rpm/60*360*Time.deltaTime ,0,0);
    
            //changing tyre direction
            Vector3 temp = WheelFLtrans.localEulerAngles;
            Vector3 temp1 = WheelFRtrans.localEulerAngles;
    
            temp.y = WheelFL.steerAngle - (WheelFLtrans.localEulerAngles.z);
    
            WheelFLtrans.localEulerAngles = temp;
    
            temp1.y = WheelFR.steerAngle - WheelFRtrans.localEulerAngles.z;
    
            WheelFRtrans.localEulerAngles = temp1;
    
            eulertest = WheelFLtrans.localEulerAngles;
        }
    
        void HandBrake()
        {
            //Debug.Log("brakes " + braked);
            if(Input.GetButton("Jump"))
            {
                braked = true;
            }
            else
            {
                braked = false;
            }
    
            if(braked){
             
                WheelRL.brakeTorque = maxBrakeTorque * 20;//0000;
                WheelRR.brakeTorque = maxBrakeTorque * 20;//0000;
    
                WheelRL.motorTorque = 0;
                WheelRR.motorTorque = 0;
    
            }
        }
    }


     

    Car Controller Script

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: