OK, so, I’ve tried to make a script that is attached to a rigid body(the interior of a vehicle), and just make it so, so you can drive, guess what, you may say whatever you want, but if you test it, you’ll see it works out pretty well(I am saying this because the script is pretty short).The problem is that I want you to tell me how to script the brakes in a better way.And also, how to make the vehicle move forward according to the CarSpeed, I mean, in game everything works perfect, JUST THAT when I press W, the car starts slowly(as it’s supossed to do), but then just simply flies, and if I collide on a fence or something, well, ACCIDENT, boom
.Here’s the code:
using UnityEngine;
using System.Collections;
public class VehicleScript : MonoBehaviour
{
public float CarSpeed;
public float MaxCarSpeed;
public GameObject Car;
public Vector3 CurPos;
public Vector3 LastPos;
void Start()
{
Car=GameObject.Find ("/Interior");
CarSpeed=0.0f;
MaxCarSpeed=1.5f;
}
void Update()
{
rigidbody.drag=0.0f;
CurPos=rigidbody.position;
if(Input.GetKey(KeyCode.W))
{
if(CarSpeed!=MaxCarSpeed)
{
CarSpeed+=0.1f;
rigidbody.AddForce(transform.forward*CarSpeed,ForceMode.VelocityChange);
}
}
if(Input.GetKey(KeyCode.A))
rigidbody.transform.Rotate(Vector3.down * CarSpeed);
if(Input.GetKey(KeyCode.D))
rigidbody.transform.Rotate(Vector3.up * CarSpeed);
if(Input.GetKey(KeyCode.S))
{
if(CurPos!=LastPos)
{
rigidbody.drag=2.0f;
CarSpeed-=0.1f;
}
}
LastPos=CurPos;
}
}
The car just simply, BOOM, takes off like a rocket, even if the speed is small.