Cyclist detecting player

I’m working on a game and I have a my player with the tag “Player”. I have a cyclist spawning system, and each cyclist has the tag “cyclist”, they are all prefabs. I have a script on the cyclists prefab that makes it move forwards, so each time it spawns, it instantly begins to move in a fixed direction.

I want the cyclists to be able to detect 2 things, one being if the player is in front of it, and the other if another cyclist is in-front of it. If so, I want the cyclist to stop cyclist. I have a script called cyclistStoping.cs that I’m using to do this. The script has been placed onto the cyclist prefab for multiple instances to be spawned.

Bug

I’ve noticed that sometimes even once a cyclist(a) is no longer in front of another cyclist(b), cyclist(b) will still remain still, and it will only move off again if the player walks in front of it and then walks off again. I feel like my code is just buggy and would really appreciate some help on this.

I’ve tried to do 2 simple checks inside each trigger function but some cyclists still remain idle when once the cyclist is no longer in front of them.

I have also tried using a delay function so whenever the cyclist or player are no longer in range, the cyclist doesn’t move off until 2-3 seconds have passed. However with this, the cyclist doesn’t move off again.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class cyclistStoping : MonoBehaviour
 {

 public VehicleMove cyclistMovement;


 public bool playerInside = false;
 public bool cyclistInside = false;

 private bool timePassed = false;

 void Start()
 {

 }

 void Update()
 {

 }

 // implement delay

 // player or cyclist (INSIDE)
 private void OnTriggerEnter(Collider other)
 {
 if (other.gameObject.tag == "Player" ) {
     playerInside = true;
     cyclistInside = false;
 }

 else if (other.gameObject.tag == "Cyclist")
 {
     playerInside = false;
     cyclistInside = true;
 }

 if (playerInside == true || cyclistInside == true) {
     Debug.Log("Player inside: " + playerInside);
     cyclistMovement.vehicleMovement = 0.0f;

 }
 }

 // player or cyclist (EXIT)
 private void OnTriggerExit(Collider other)
 {
 if (other.gameObject.tag == "Player")
 {
     playerInside = false;
     cyclistInside = false;
 }

 else if (other.gameObject.tag == "Cyclist")
 {
     playerInside = false;
     cyclistInside = false;
 }

 if (playerInside == false || cyclistInside == false)
 {

     if (timePassed == true) {
         Debug.Log("Player inside: " + playerInside);
         cyclistMovement.vehicleMovement = 0.1f;
         // delay, then move off
         timePassed = false;
     }
 }
 }

 IEnumerator timeDelay()
 {
     // wait before moving off
     yield return new WaitForSeconds(3);
     timePassed = true;
     }

     }

I expect the cyclist to stop if a player is in front of it, and once the next cyclist spawn, I expect THAT cyclist to stop once it detects the first cyclist. Once the player moves away from the first cyclist, it should take 2 seconds before moving off, and the second cyclist will do exactly the same.

if (playerInside == false || cyclistInside == false)
{

      if (timePassed == true) {
          Debug.Log("Player inside: " + playerInside);
          cyclistMovement.vehicleMovement = 0.1f;
          // delay, then move off
          timePassed = false;
      }

It feels weird that if you want the cyclist to start moving again when there is no other cyclist and player … why you have OR in your if statement ? It should be && instead of ||.

Also … when you are setting the booleans in OnTriggerEnter

  if (other.gameObject.tag == "Player" ) {
      playerInside = true;
      cyclistInside = false;
  }

You cant assume that if the player entered there is no cyclist inside … what if there was a cyclist already inside ? Same goes for the next if statement.

Also i do not see you using your timeDelay function …

I would change it to

if (playerInside == false && cyclistInside == false)
  {
     StartCoroutine(timeDelay());
  }

and then the timeDelay() to :

 IEnumerator timeDelay()
  {
      yield return new WaitForSeconds(yourwaittime);
      //reset speed of cyclist.
  }

Thats all i could help with, as i am currently still at work.
Can help you more later. Good luck !