How to create a script to make pedestrians respond to traffic lights?

How to create a script to make pedestrians respond to traffic lights?

fake code:

Class Environment
{
   public List<Trafficlights> trafficlights
}

Class People
{
   public Environment env;

   Update()
   {
     
     for each env.trafficlights
            if(trafficlight.isRed)
                     Stop()
   }

}

Create a dedicated Box Collider trigger around these lights and make pedestrians test for light state with OnTriggerEnter and stop crossing when it’s red & yellow.

Add this to a Box Collider that is set to Is Trigger ☑ and place near where road crossing starts:

public class TrafficLights : MonoBehaviour
{
    [SerializeField] EState _state = EState.Red;
    public EState State => _state;
    public enum EState : byte { Red=0, Yellow=1, Green=2 }
}

This is part of the script that controls the NavMeshAgent:

public class Pawn : MonoBehaviour
{
    void OnTriggerEnter ( Collider other )
    {
        if( other.TryGetComponent<TrafficLights>( out var trafficLights ) )// is this traffic `TrafficLights` trigger
        if( trafficLights.State!=TrafficLights.EState.Green )// are lights red or yellow
        {
            StopMoving();
        }
    }
}

I seem to have errors in the above codes

The error seems to be that the current context does not exist

This is my traffic light control script:

// paste code here

My codes does not seem to work

Sorry for the inconvenience, as I just managed to get the system working. However, may I ask how to make my character stop walking using my current codes?
Could you help me correct them. Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.VFX;

public class CharacterNavigationController : MonoBehaviour
{
    public float movementSpeed = 1;
    public float rotationSpeed = 120;
    public float stopDistance = 2f;
    public Vector3 destination;
    public Animator animator;
    public bool reachedDestination;

    private Vector3 lastPosition;
    Vector3 velocity;
        
    private void Awake()
    {
        movementSpeed = Random.Range(0.5f, 2f);
        animator      = GetComponent<Animator>();
    }

    //Update is called once per frame
    void Update()
    {

        if(transform.position != destination)
        {
            Vector3 destinationDirection = destination - transform.position;
            destinationDirection.y = 0;
            float destinationDestance = destinationDirection.magnitude;
            if (destinationDestance >= stopDistance)
            {
                reachedDestination = false;
                Quaternion targetRotation = Quaternion.LookRotation(destinationDirection);
                transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
                transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
            }
            else
            {
                reachedDestination = true;
            }

            velocity = (transform.position - lastPosition) / Time.deltaTime;
            velocity.y = 0;
            var velocityMagnitude = velocity.magnitude;
            velocity = velocity.normalized;
            var fwdDotProduct = Vector3.Dot(transform.forward, velocity);
            var rightDotProduct = Vector3.Dot(transform.right, velocity);

            animator.SetFloat("Horizontal", rightDotProduct);
            animator.SetFloat("Vertical", fwdDotProduct);
           
        }

        lastPosition = transform.position;
    }

    public void SetDestination(Vector3 destination)
    {
        this.destination = destination;
        reachedDestination = false;
    }

    void OnTriggerEnter ( Collider other )
    {
        if (other.TryGetComponent<TrafficLights>(out var trafficLights))
        if (trafficLights.State != TrafficLights.EState.Green)
            {
                print ("Enter");
            }
    }



}

I seem to have another issue with the above codes. When the lights are green, the message should display “enter”, meaning cannot cross.

However, when I tried running a few times, it seems to always generate the message no matter what the light is (red/green).

Hi again @MeganPein. If you still don’t know how to do this I made a repo that will show you how to implement such behavior as a state machine
GIF 11.02.2024 13-00-52

Link to specific implementation sample

Simpletons/Samples/TrafficLights at main · andrew-raphael-lukasik/Simpletons · GitHub

Repo main page

GitHub - andrew-raphael-lukasik/Simpletons