我目前正在创建停车场和驾驶模拟游戏,但制作上遇到了问题。目前我已成功实施
问题是,如果我的车在红绿灯亮时前进,然后突然在另一个红绿灯上变成红色,它将触发下一个对撞机,我不希望这样,因为这是一个错误对? 。请参见下图。
StopAndGoHandler游戏对象具有名为 StopAndGoHandler.cs 的脚本 如果交通信号灯为绿色,则可以执行此操作,黄色准备停止,但红色仍可以继续运行。现在,即使交通信号灯为红色,如果汽车仍在行驶,那么
违反游戏对象,该脚本具有脚本 TrafficViolation.cs
这是 StopAndGoHandler.cs
public GameObject tlc;
public Text[] Obey_Traffic_Rules;
private void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("Player")){
TrafficLightSignal();
}
}
private void OnTriggerExit(Collider other){
if(other.gameObject.CompareTag("Player")){
TrafficRulesInit();
}
}
public void TrafficLightSignal(){
var TLC = tlc.GetComponent<TrafficLightHandler>();
Debug.Log (TLC.Trafficlight.ToString());
if (TLC.Trafficlight == TRAFFIC_LIGHT.GREEN) {
TrafficRules(Obey_Traffic_Rules[0], true);
TrafficRules(Obey_Traffic_Rules[1], false);
TrafficRules(Obey_Traffic_Rules[2], false);
TrafficRules(Obey_Traffic_Rules[3], false);
} else if (TLC.Trafficlight == TRAFFIC_LIGHT.YELLOW) {
TrafficRules (Obey_Traffic_Rules [1], true);
TrafficRules(Obey_Traffic_Rules[2], false);
TrafficRules(Obey_Traffic_Rules[3], false);
TrafficRules(Obey_Traffic_Rules[0], false);
} else if (TLC.Trafficlight == TRAFFIC_LIGHT.RED) {
TrafficRules(Obey_Traffic_Rules[2], true);
TrafficRules(Obey_Traffic_Rules[0], false);
TrafficRules(Obey_Traffic_Rules[1], false);
TrafficRules(Obey_Traffic_Rules[3], false);
} else {
// nothing
}
}
public void TrafficRules(Text t, bool val){
t.gameObject.SetActive (val);
}
public void TrafficRulesInit()
{
TrafficRules(Obey_Traffic_Rules[0], false);
TrafficRules(Obey_Traffic_Rules[1], false);
TrafficRules(Obey_Traffic_Rules[2], false);
TrafficRules(Obey_Traffic_Rules[3], false);
}
这是我的 TrafficViolation.cs ,继承了 StopAndGoHandler.cs
void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("Player")){
_TrafficViolation();
}
}
private void _TrafficViolation(){
var TLC = tlc.GetComponent<TrafficLightHandler>();
if (TLC.Trafficlight == TRAFFIC_LIGHT.RED) {
TrafficRules (Obey_Traffic_Rules [3], true);
TrafficRules (Obey_Traffic_Rules [0], false);
TrafficRules (Obey_Traffic_Rules [1], false);
TrafficRules (Obey_Traffic_Rules [2], false);
} else {
TrafficRules (Obey_Traffic_Rules [0], true);
TrafficRules (Obey_Traffic_Rules [1], false);
TrafficRules (Obey_Traffic_Rules [2], false);
TrafficRules (Obey_Traffic_Rules [3], false);
}
}
顺便说一下,TLC是我的TrafficLightSystem,它只有一个枚举
public enum TRAFFIC_LIGHT
{
GREEN,
YELLOW,
RED
};
我的 TrafficLightHandler.cs 只是将球体的材质从红色,绿色和黄色更改。
如果我没有正确执行此游戏,请有人帮助我,否则请指导我。谢谢。
答案 0 :(得分:1)