- Home /
Making character respond to traffic light
I'm trying to build a traffic scene in the unity. i have an avatar as pedestrian and it walks along the sidewalk & uses the pedestrian crossing to cross the road. However, i also want the pedestrian to respond to the traffic light so that it wont cross the road when there's a red light. is there any script that i need to write or is there any built in component in the unity engine that could make an object respond to another object? Im still new to this engine & kind of not know where to start. Help me please! thank you!
Answer by zxc76_die · Nov 04, 2018 at 04:27 AM
Create an invisible cube at a pedestrian crossing. Make him a colider trigger. And add this object through the inspector in the object of the traffic light. GameObject MyTrafficColiderObject;
From the traffic light send the variable "bool stand" to this invisible cube.
Through OnTriggerStay, give the pedestrian information that you cannot go ("bool stop = false")
Like that. Sorry for translation difficulties. :)
Answer by njwashaiful · Nov 04, 2018 at 06:56 PM
Thank you @zxc76_die for the reply! ok, ive created a cube and added it in front of the cross road. however i couldnt seem to add this object to the traffic light object. this is the script of the traffic light: using UnityEngine; using System.Collections;
//=============================================================================
// HaloTrafficLight
// by Healthbar Games (http://healthbargames.pl)
// author: Mariusz Skowroński
//
// Simple implementation of TrafficLight
// For each of three light colors (red, yellow and green) it uses
// one mesh renderer and one object with halo effect attached.
// To visualize the states of lights (on / off) it requires two materials:
// - one with a texture for the lights turned off
// - and one with a texture for the lights turned on.
// You can use (like in demo scene) two different materials with single,
// common texture for light states.
//=============================================================================
namespace HealthbarGames
{
public class HaloTrafficLight : TrafficLightBase
{
public Renderer RedRenderer;
public GameObject RedHalo;
public Renderer YellowRenderer;
public GameObject YellowHalo;
public Renderer GreenRenderer;
public GameObject GreenHalo;
public Material LightsOnMat;
public Material LightsOffMat;
private bool mInitialized = false;
void Awake()
{
if ( (RedRenderer != null || RedHalo != null)
&& (YellowRenderer != null || YellowHalo != null)
&& (GreenRenderer != null || GreenHalo != null)
)
{
mInitialized = true;
}
else
{
mInitialized = false;
Debug.LogError("Some variables haven't been assigned correctly for HaloTrafficLight script.", this);
}
}
// implementation of the callback from TrafficLight - called when lights state has changed
public override void OnLightStateChanged(bool redLightState, bool yellowLightState, bool greenLightState)
{
if (!mInitialized)
return;
if (RedHalo != null)
RedHalo.SetActive(redLightState);
if (RedRenderer != null)
RedRenderer.material = (redLightState) ? LightsOnMat : LightsOffMat;
if (YellowHalo != null)
YellowHalo.SetActive(yellowLightState);
if (YellowRenderer != null)
YellowRenderer.material = (yellowLightState) ? LightsOnMat : LightsOffMat;
if (GreenHalo != null)
GreenHalo.SetActive(greenLightState);
if (GreenRenderer != null)
GreenRenderer.material = (greenLightState) ? LightsOnMat : LightsOffMat;
}
}
}
so if i add public GameObject TrafficLightCollider; , how do i send the variable bool stand to this collider?