英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论