I made my own car in 3ds max, and imported it to my unity project. So, i want it to drive, so i dragged all the scripts the car tutorial car have, and added a ridgidbody. but it still wont drive? What do i do wrong?
This below is the most basic car script that can be.
You need to add four wheel colliders to your car and position them at the four wheel with appropriate dimension. Then drag and drop into the corresponding wheel collider variables of your script.
Give some mass to the rigidbody, some values to the steer, motor and brake and roll on.
You may have to tweak your center of mass until your car stops flipping. Also, make usre the wheel collider friction values are set to a low value like 0.02/0.03 or your car will flip as soon as your steer.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class MovementCar : MonoBehaviour {
public WheelCollider frontLeft;
public WheelCollider frontRight;
public WheelCollider backLeft;
public WheelCollider backRight;
Rigidbody _rigidbody;
Transform _transform;
Vector3 _currentRotation;
public float steer_max = 20f,motor_max=10f,brake_max=100;
float steer = 0f,motor=0f,brake=0f;
void Awake(){
_rigidbody = rigidbody;
_transform = transform;
_currentRotation = new Vector3 (transform.localEulerAngles.x,
transform.localEulerAngles.y, transform.localEulerAngles.z);
}
void Start () {
_rigidbody.centerOfMass = new Vector3(0.0f,-0.95f,0.0f);
}
void FixedUpdate () {
steer = Input.GetAxis("Horizontal");
motor = Mathf.Clamp(Input.GetAxis("Vertical"),0,1);
brake = Mathf.Clamp(Input.GetAxis("Vertical"),-1,0);
frontLeft.motorTorque = motor_max*motor;
frontRight.motorTorque = motor_max*motor;
frontLeft.brakeTorque = brake_max * brake;
frontRight.brakeTorque = brake_max*brake;
frontLeft.steerAngle = steer_max*steer;
frontRight.steerAngle = steer_max *steer;
}
void LateUpdate()
{
if (steer == 0){
float y = _currentRotation.y;
_transform.localEulerAngles = new Vector3 (transform.localEulerAngles.x, y, transform.localEulerAngles.z);
}else
_currentRotation.y = transform.localEulerAngles.y;
}
void OnGUI(){
GUI.Box(new Rect(50,50,200,50),(CarVelocity ()).ToString("0")+"km/h");
}
float CarVelocity(){
return 2f * Mathf.PI*backLeft.radius *backLeft.rpm*60f/1000f;
}
}
Hmm… I tried Javascript and C# but it dosen’t work… it says Assets/CarMotor.js(1,6): UCE0001: ‘;’ expected. Insert a semicolon at the end.