Hello people, im getting some issues with my script, i have a prefab called Car, i instantiate in some positions in the streets of the city, and we have semaphores, i have a script who can check every frame in Update() if the car have some collider in front he stop. But the car stop, and the other hits the car ahead, then the other stops, then the other hits, stop, hit, stop, hit, i dont know what is going on.
I want only the cars to stop in every collider.
void Update(){
if(carSpeed){
transform.Translate(0,0,Time.deltaTime * -10);
}
if(collideDetect){
carSpeed = false;
}
else{ carSpeed = true; }
RaycastHit hit;
if (Physics.Raycast(transform.position, new Vector3(0,0,20), out hit))
{
if(hit.collider.gameObject.name.Contains("Semaphore Trigger") && (hit.distance < 2.5f) ||
hit.collider.gameObject.tag.Contains("Car") && (hit.distance < 2.5f))
{
collideDetect = true;
}
}
else { collideDetect = false; }
}
someone have an idea?