In my project I have a car, with a script attached to it, for driving. In my world are traffic lights, if it is red, there spawns an object (invisible, named stop) that makes the car stop if its there, and if it is away it goes on driving with a specific variable (speed). How can I script a deceleration, and acceleration? Tried to manage it with Wait for Seconds and external coroutines, but nothing worked. In the update void:
Ray disray = new Ray(transform.position, transform.forward);
RaycastHit dishit;
if (Physics.Raycast(disray, out dishit, 8) && dishit.transform.tag == "stop")
{
if (dishit.distance < carrange)
{
transform.Translate(0, 0, 0);
}
}
else
{
transform.Translate(0, 0, speed * Time.deltaTime);
}
Thanks!