【问题标题】:Unity Trigger Collision not workingUnity触发器碰撞不起作用
【发布时间】:2020-09-04 12:24:39
【问题描述】:

我目前正在使用 Unity3D 开发一个简单的赛车游戏。玩家是一辆赛车,与使用航路点在地图上导航的 AI 比赛。每次玩家越过终点线时,计圈器都会增加一圈。我希望 AI 计圈器也发生同样的事情,但是一旦 AI 越过终点线,什么都不会发生。甚至 Debug.Log 也没有显示任何正在发生的事情。我有一个项目 here 的 tinyurl。

您将在下面找到用于触发计圈器的代码。

感谢任何帮助。

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;

    }
}

【问题讨论】:

  • 我已经看过无数次了。我需要你提供的信息来解决它:碰撞器、刚体和 AI 代码。
  • 是否选中了触发器中的 IsTrigger 选项?物体有刚体吗?
  • IsTrigger 已检查,两辆赛车都有刚体。如果您在我之前的帖子中按“此处”,您可以获得所有您需要自己查看的文件

标签: unity3d collision


【解决方案1】:

我不确定AITriggerEnter 是否会被调用。

OnTriggerEnter 将在带有碰撞器的刚体进入 isTrigger 设置为 true 的碰撞器时调用。

不要有两个方法,而是在OnTriggerEnter 中添加所有逻辑,或者至少确保从那里调用方法,例如

void OnTriggerEnter(Collider other) {
    bool isPlayer = other.compareTag("Buggy");
    bool isAI = other.compareTag("AIBuggy");
    if (!isPlayer && !isAI) return;
    
    if (isPlayer) {
        lapcount++;
    }
    else if (isPlayer) {
        AIlapcount++;
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多