OnTriggerEnter 在 Unity 2D 中不触发,尽管满足所有要求?

huangapple go评论87阅读模式
英文:

OnTriggerEnter doesn't activate although all the requirements are met in Unity 2D?

问题

我编写了这个简单的Unity脚本,它应该在与某物发生碰撞时在控制台中输出信息,但似乎不起作用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollisionScript : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
Debug.Log("与" + other.gameObject.name + "发生了触发器碰撞");
}
}


我确保了分配了此脚本的对象以及此对象应该接触的对象都具有RigidBody 2D和Box Collider 2D,并且它们的Box Collider已激活了“IsTrigger”。
英文:

I wrote this simple unity script that is supposed to write in console when it collides with something but it doesn't seem to work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollisionScript : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
        Debug.Log("Triggered with " + other.gameObject.name);
    }
}

I made sure that both the object that this script is assigned to and the object that this object is supposed to thouch have a RigidBody 2D and a Box Collider 2D and that their Box Colliders have "IsTrigger" activated.

答案1

得分: 1

你的脚本使用了OnTriggerEnter,这在3D项目中是正确的,但因为你在2D环境中工作,所以需要使用OnTriggerEnter2D,代码如下:

using UnityEngine;

public class CollisionScript : MonoBehaviour
{
    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("与物体触发:" + other.gameObject.name);
    }
}
英文:

Your script uses OnTriggerEnter, which is correct for 3D projects, but because you're working in 2D, you need to use OnTriggerEnter2D as follows:

using UnityEngine;

public class CollisionScript : MonoBehaviour
{
    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log("Triggered with " + other.gameObject.name);
    }
}

huangapple
  • 本文由 发表于 2023年7月10日 23:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定