Cyclist script bug?
I have previously asked about this question, and whilst doing some debugging, I have finally found the issue with my code, however I'm not sure how to go about fixing it.
I have created a spawn system that spawns cyclists and instantly begin to cycle them forwards, I have 1 script that makes each cyclist move forwards, and another that makes them stop and continue off when another player or cyclist is in front of it, THIS is the script that has the issue.
To quickly summarise, whenever a cyclist(a) is stopped by a player, and another cyclist(b) get stopped by the cyclist in front, a boolean triggers which turns playerInside and cyclistInside to true. This is fine. However once the player moves out the way, the first cyclist(a) has playerInside set to false, which is correct, but cyclistInside is still set to true. This is because the cyclist behind him is still detecting the one in front. So it doesn't seem like the scripts are working independently for each cyclist.
I've spent some time debugging to really find the issue here, and this is what I've found.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cyclistStoping : MonoBehaviour
{
public VehicleMove cyclistMovement;
public Animator CycleAnims;
// bools
public bool playerInside;
public bool cyclistInside;
private void Start()
{
playerInside = false;
cyclistInside = false;
}
// implement delay
private void Update()
{
if (playerInside == false && cyclistInside == false)
{
StartCoroutine(timeDelay());
}
}
// player or cyclist (INSIDE)
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player" ) {
playerInside = true;
cyclistMovement.vehicleMovement = 0.0f;
}
else if (other.gameObject.tag == "Cyclist")
{
cyclistInside = true;
cyclistMovement.vehicleMovement = 0.0f;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
playerInside = false;
}
else if (other.gameObject.tag == "Cyclist")
{
cyclistInside = false;
}
}
IEnumerator timeDelay()
{
// wait before moving off
yield return new WaitForSeconds(2);
if (playerInside == false && cyclistInside == false)
{
cyclistMovement.vehicleMovement = 0.1f;
StopAllCoroutines();
}
}
}
At this point, once the player moves away from the cyclist, the first cyclist (and the ones waiting behind) won't move off, because they are waiting for the first cyclist to move, which won't move due to the cyclistInside being set to true even though there's no cyclist infront.
I expect the cyclist to move off once the player is no longer in front of it, and so fourth with the rest of them.
Your answer
Follow this Question
Related Questions
How to make a "raycast" vertically in front of the player ? 0 Answers
How to create a semi-intelligent trigger system? 0 Answers
Changing the speed of transform.translate? 3 Answers
unity create scene from script and pass information between scenes 0 Answers
UI Text: Words at end of line jumping to next line 0 Answers