简体   繁体   English

unity OnTriggerEnter2D 未注册

[英]Unity OnTriggerEnter2D not registering

I am new to using unity and am having a real problem getting trigger collisions to register.我刚开始使用 unity,在注册触发碰撞时遇到了一个真正的问题。

public void  OnTriggerEnter2D(Collider2D other)
{
    print("collide");
    if (other.CompareTag("Fish"))
    {
        print("Caught");
    }
}

I have 2D polygon colliders and a rigid body on both items.我在这两个项目上都有 2D 多边形碰撞器和刚体。 I have also got 1 set a trigger(have tried having both as trigger).我还设置了 1 个触发器(已尝试将两者都设置为触发器)。 However one UI item is a sprite and the other is an image.然而,一个 UI 项目是精灵,另一个是图像。 Both items are also tagged with "fish"这两项也都标有“鱼”

Would really appreciate any help.非常感谢任何帮助。 Thanks谢谢

There are four things I can think of which need to happen so that OnTriggerEnter gets called:我可以想到需要发生四件事,以便调用OnTriggerEnter

  1. The two objects' colliders need to actually be overlapping.这两个对象的碰撞器实际上需要重叠。 Just because their rendered pixels are overlapping, doesn't mean their colliders are overlapping.仅仅因为它们的渲染像素重叠,并不意味着它们的碰撞体重叠。 This can be checked in the editor.这可以在编辑器中检查。 Their colliders are indicated with a green outline in the Scene tab.它们的碰撞器在“ Scene选项卡中用绿色轮廓表示。 If you don't see anything, make sure the button labeled Gizmos is turned on.如果您没有看到任何东西,请确保标有Gizmos的按钮已打开。

  2. The two objects need to be in physics layers which are set to collide with each other.这两个对象需要位于设置为相互碰撞的物理层中。 You can check this in Edit > Settings > Physics2D > Layer Collision Matrix .您可以在Edit > Settings > Physics2D > Layer Collision Matrix中查看。

  3. Both objects need to have Rigidbody2D components attached.这两个对象都需要附加 Rigidbody2D 组件。 Make sure these aren't Rigidbody components, as those are for 3D physics.确保这些不是刚体组件,因为它们适用于 3D 物理。

  4. The object which contains the OnTriggerEnter2D event needs to have isTrigger = true .包含OnTriggerEnter2D事件的 object 需要具有isTrigger = true

I've tried several things to do.我已经尝试了几件事。 First I've checked recommendations from another post.首先,我检查了另一篇文章的建议。 In nutshell:简而言之:

  1. Checked if I have rigid body at least for one of objects检查我是否至少有一个物体有刚体
  2. Checked layers检查图层
  3. Checked tags检查标签
  4. Played with "is trigger".播放“是触发器”。

Finally, the solution was to add script to object via create and add component button and not to drop written script to it.最后,解决方案是通过创建和添加组件按钮将脚本添加到 object,而不是将编写的脚本放到其中。 Have no idea, but for me that was the solution.不知道,但对我来说这就是解决方案。 Even it was same script.即使它是相同的脚本。

Both a Sprite and an Image can collide with another Image. Sprite 和 Image 都可以与另一个 Image 发生碰撞。 What might be wrong is that your sprite may look like touching the image however in the scene the canvas might be far away, so camera may deceive you.可能有问题的是,您的精灵可能看起来像是在触摸图像,但在场景中 canvas 可能距离很远,因此相机可能会欺骗您。 Here is the sample code for my tests:这是我的测试示例代码:

Script that moves the Sprite:移动精灵的脚本:

    private Rigidbody2D _rigidbody;
    private void Awake() => _rigidbody = GetComponent<Rigidbody2D>();

        private void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.A))
        {
            var movement = -transform.right * Time.fixedDeltaTime * 250;
            _rigidbody.MovePosition(transform.position + movement);
        }
        
        if (Input.GetKey(KeyCode.D))
        {
            var movement = transform.right * Time.fixedDeltaTime * 250;
            _rigidbody.MovePosition(transform.position + movement);
        }
    }

Trigger script on image:图像上的触发脚本:

    private void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log(other.name);
    }

Nothing helps for me, but in my case I didn't know there was a place in code that breaks collision layers (that is do not change the collision matrix settings visually):对我没有任何帮助,但就我而言,我不知道代码中有一个地方会破坏碰撞层(即不要在视觉上更改碰撞矩阵设置):

Physics2D.IgnoreLayerCollision( ... Physics2D.IgnoreLayerCollision( ...

Check that too and make sure it is not called.也要检查并确保它没有被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM