Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

car controller script unity

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;
        }
    }
}
Comment

car controller Unity

using UnityEngine;

public class car_controller : MonoBehaviour
{
    public WheelCollider[] wheel_col;
    public Transform[] wheels;
    float torque=100;
    float angle=45;
    
    void Update()
    {
        for(int i=0;i<wheel_col.Length;i++)
        {
            wheel_col[i].motorTorque=Input.GetAxis("Vertical")*torque;
            if(i==0||i==2)
            {
                wheel_col[i].steerAngle=Input.GetAxis("Horizontal")*angle;
            }
            var pos=transform.position;
            var rot=transform.rotation;
            wheel_col[i].GetWorldPose(out pos,out rot);
            wheels[i].position=pos;
            wheels[i].rotation=rot;
            
        }
        if(Input.anyKeyDown) 
        {
            if(Input.GetKeyDown(KeyCode.Space))
            {
                foreach(var i in wheel_col)
                {
                    i.brakeTorque=2000;
                }
            }
            else{   //reset the brake torque when another key is pressed
                foreach(var i in wheel_col)
                {
                    i.brakeTorque=0;
                }
                
            }
        }
        
       
        
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to get properties from json in c# 
Csharp :: c# anonymous type as return value 
Csharp :: ef core many to many fluent api 
Csharp :: c# string across multiple lines 
Csharp :: c# convert xml to list string 
Csharp :: c# string verbatim 
Csharp :: how to return array in function c# 
Csharp :: C# Console font 
Csharp :: set background from C# wpf 
Csharp :: by value by reference c# 
Csharp :: escape in c# 
Csharp :: delete selected cells in Datagridview 
Csharp :: matrix transpose c# 
Csharp :: How to create a class and objects in C# 
Csharp :: how to lerp a value in unity 
Csharp :: how to auto format c# code in visual studio 
Csharp :: C# date type no time 
Csharp :: .net core web api save pdf file in local folder 
Csharp :: active form 
Csharp :: how to solo squad in fortnight 
Csharp :: no cameras rendering unity 
Csharp :: c# linq sorting sequential guids 
Csharp :: how many zeros in quinnonagintillion 
Csharp :: c# execute after delay 
Csharp :: loop code for X seconds 
Csharp :: ef core index attribute 
Csharp :: control shot c# WF 
Csharp :: how to learn c# fast to learn unity 
Csharp :: How do I remove a String Array from a List in C# 
Csharp :: paging thru result from mongodb in C# 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =