我目前正在创建一个停车场和驾驶模拟游戏,但我遇到了问题。目前我成功实施了什么
问题是,如果我的车在交通信号灯为绿色时向前行驶,然后在其他红绿灯处突然变为红色,则会触发下一个对撞机,我不希望这样,因为这是一个错误吗? 。请看下面的图片。
StopAndGoHandler GameObject有一个名为StopAndGoHandler.cs 的脚本,如果交通信号灯是绿色你可以去,黄色准备停止,但你仍然可以去,红色完全停止。现在,如果汽车前进,即使交通信号灯是红色的,那么它就会出现
违反GameObject,其中包含脚本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);
}
这是继承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只是改变了红色,绿色和黄色的球体材质。
如果我正确地做这个游戏,有人请帮助我,如果不能请你指导我。谢谢。