Unity Trigger Collision无法正常工作

时间:2014-08-18 14:10:56

标签: unity3d collision

我目前正在Unity3D中开展简单的比赛。玩家是赛车,与人工智能竞赛,使用航路点在地图上导航。每当玩家越过终点线时,一个圈子就会增加一圈。我想要同样的情况发生在AI圈计中,但是一旦AI越过终点线,没有任何反应。甚至Debug.Log都没有显示发生的任何事情。我对项目here有一个小小的了解。

在您的下方,我会找到用于触发笔记本电脑的代码。

感谢任何帮助。

using UnityEngine;
using System.Collections;

public class StartTrigger : MonoBehaviour {

    public Light StartFlash;
    public int lapcount=0;
    public int AIlapcount=0;
    public int ctimer=10;
    public int AIctimer=10;
    public float gametime;
    public float timedisp;

    // Use this for initialization
    void Start () {
        StartFlash.intensity =0;
        gametime = 0;
    }

    // Update is called once per frame
    void Update () {
        gametime+=Time.deltaTime;
    }


    void OnTriggerEnter( Collider MyTrigger){
        if ((MyTrigger.gameObject.tag == "Buggy") && (lapcount == 0)) {
            Debug.Log ("Trigger passed");
            lapcount = 1;
            ctimer = 10;
        }
        ctimer -= 2;

        if ((MyTrigger.gameObject.tag == "Buggy") && (lapcount == 1) && (ctimer <= 0)) {
            lapcount = 2;
            ctimer = 10;
        }
        ctimer -= 2;

        if ((MyTrigger.gameObject.tag == "Buggy") && (lapcount == 2) && (ctimer <= 0)) {
            lapcount = 3;
            ctimer = 10;
        }
        if ((MyTrigger.gameObject.tag == "Buggy") && (lapcount == 3) && (ctimer <= 0)) {
            Application.LoadLevel (0);
        }
    }

    void AITriggerEnter( Collider Trigger){
        Debug.Log ("AI Trigger passed");
        if ((Trigger.gameObject.tag == "AIBuggy") && (AIlapcount ==0)) {
            AIlapcount =1;
            AIctimer=10;
        }
        AIctimer-=2;

        if((Trigger.gameObject.tag == "AIBuggy") && (AIlapcount == 1) && (AIctimer <= 0)) {
            AIlapcount =2;
            AIctimer=10;
        }
        AIctimer-=2;

        if((Trigger.gameObject.tag == "AIBuggy") && (AIlapcount == 2) && (AIctimer <= 0)) {
            AIlapcount =3;
            AIctimer=10;
        }
        if((Trigger.gameObject.tag == "AIBuggy") && (AIlapcount == 3) && (AIctimer <= 0)) {
            Application.LoadLevel(0);
        }


    } // end OnTriggerEnter

    void OnGUI(){

        //PlayerCarScript myCar = mPlayer.GetComponent <PlayerCarScript> ();

        GUI.BeginGroup (new Rect (10, 10, 200, 140));

            GUI.Box (new Rect (0, 0, 200, 110), "User Interface");

            GUI.TextField (new Rect (0, 30, 100, 25), "Laps: " + lapcount);
            GUI.TextField (new Rect (0, 55, 100, 25), "Game Time: " + gametime.ToString("f1"));
            GUI.TextField (new Rect (0, 80, 100, 25), "Timer: " + ctimer);

        GUI.EndGroup ();

        GUI.BeginGroup (new Rect (600, 10, 200, 140));

        GUI.Box (new Rect (0, 0, 200, 110), "Opponent Interface");

        GUI.TextField (new Rect (0, 30, 100, 25), "Laps: " + AIlapcount);
        //GUI.TextField (new Rect (0, 55, 100, 25), "Game Time: " + gametime.ToString("f1"));
        GUI.TextField (new Rect (0, 80, 100, 25), "Timer: " + AIctimer);

        GUI.EndGroup ();
    }

    void Flash(){

        StartFlash.intensity = 8 - StartFlash.intensity;

    }
}

1 个答案:

答案 0 :(得分:0)

我不确定是否会fetch()被呼叫。

当具有碰撞体的刚体进入设置为true的AITriggerEnter的碰撞体时,将调用

OnTriggerEnter

与其使用两个方法,不如在isTrigger内添加所有逻辑,或者至少确保从那里调用方法,例如

OnTriggerEnter

不确定计时器的用途,但这似乎是要使用的计时器的简单版本