Im very new to coding so bear with me, im making a car game, with the car being a single block, so no fancy wheels or anything.
I have the movement code down, now i need to somehow make it so when i press space bar, the ‘car’ slowly decelerates to a complete stop. Can someone help?
Heres the code:
public class scr : MonoBehaviour
{
public float thrust = 1.0f;
public Rigidbody rb;
public GameObject move;
public Vector3 offset;
public float angleBetween = 0.0f;
public Transform target;
void Start()
{
rb = GetComponent<Rigidbody>();
offset = transform.position - move.transform.position;
}
//movementcode
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
rb.AddForce(transform.forward * thrust);
if (Input.GetKey(KeyCode.S))
rb.AddForce(-transform.forward * thrust);
if (Input.GetKey(KeyCode.A))
transform.Rotate(0, -1, 0 * thrust);
if (Input.GetKey(KeyCode.D))
transform.Rotate(0, 1, 0 * thrust);
if (Input.GetKey(KeyCode.LeftShift))
rb.AddForce(transform.forward * thrust * 2, ForceMode.Acceleration);
}
void LateUpdate()
{
transform.position = move.transform.position + offset;
}
}
Thank you!