I am trying to make a simple car game in Unity.
I want to make a car controller such that it can move forward and in X direction.
for moving car in X direction i am using
transform.translate.x
i can use the same for forward but i want it to accelerate its velocity relatively with time and to stop slightly on applying brake which is not possible in transform.translate.forward help me in doing this.
void Start(){
rb = GetComponent<Rigidbody>();
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space){
rb.velocity *= 0.9f; // it will slow down until it stops completly, if you multiply it by lets say 1.1f then it will accelerate
}
}
If you want to have maximum speed you can change rb.velocity.magnitude and if it increases certein speed then you can stop accelerating etc.
There’s also another solution to this. You can use Unitys lerp functions. When combined with time you can manipulate simple transforms such as transform translate. I do not have the code for this but search it up on google if you don’t want it to be physicsbased.