简体   繁体   English

Unity C# 胶囊对撞机和刚体不触发使用 transform.position 移动

[英]Unity C# capsule collider and rigid body don't trigger using transform.position to move

I have 2 simple capsules.我有 2 个简单的胶囊。 1 is stationary and the other is moving to the same tile on the tilemap using transform.position. 1 是静止的,另一个正在使用 transform.position 移动到瓦片地图上的同一个瓦片。

Both capsules have capsule colliders and rigid bodies.两个胶囊都有胶囊碰撞器和刚体。 I've attempted to remove the rigid body but from what I can tell, the OnCollisionEnter function requires a rigid body to work.我试图移除刚体,但据我所知,OnCollisionEnter function 需要刚体才能工作。

My script, attached to both of these, is a simple:我的脚本附在这两个上,很简单:

private void OnCollisionEnter(Collision other)
{
    print("Collision Detected! " + other.gameObject.name);
}

I've used combinations of 'isKinematic', 'isTrigger', placing one component before the other then reversing, ensuring I have the script attached to both objects, making sure my capsule collider is sized to the object, and looked at other people's issues to try their steps to fix my issue.我使用了“isKinematic”、“isTrigger”的组合,将一个组件放在另一个组件之前然后反转,确保我将脚本附加到两个对象,确保我的胶囊对撞机的大小为 object,并查看了其他人的问题尝试他们的步骤来解决我的问题。

Nothing seems to trigger the collision between the 2 capsules.似乎没有什么能触发两个胶囊之间的碰撞。 It's got to be something small I'm missing.它一定是我遗漏的小东西。

I have a tag on both objects and have tried things like if(other.gameObject.tag == "myTag") {...} but that's not working for me, either.我在两个对象上都有一个标签,并尝试过if(other.gameObject.tag == "myTag") {...}之类的东西,但这对我也不起作用。

Can anyone spot my mistake?谁能发现我的错误? Let me know if you need to see anything else - happy to provide images or whatever will help.让我知道您是否需要查看其他内容 - 很乐意提供图片或任何有帮助的内容。 Thanks in advance!提前致谢!

我的层次结构

胶囊网格组件

Above is the basic identical hierarchy of both capsules with components attached to the capsule mesh.上面是两个胶囊的基本相同层次结构,组件附加到胶囊网格。 Both have the parent empty objects they're stationed under.两者都有它们所在的父空对象。 (Which themselves, contain scripts but no collider or rigid body) (它们本身包含脚本但没有碰撞器或刚体)

Here is an infographic to show when a collision message will be detected by OnCollisionEnter between two objects.这是一个信息图,显示infographic OnCollisionEnter会在两个对象之间检测到碰撞消息。 Both objects will need some sort of collider, and will most likely need a Rigidbody .这两个对象都需要某种碰撞器,并且很可能需要一个Rigidbody

碰撞矩阵

You will not want to set isTrigger as that will not make it physically react to a collision, but will just detect when a collision occurs.您不会想要设置isTrigger ,因为这不会使其对碰撞做出物理反应,而只会检测碰撞何时发生。 It will also not call OnCollisionEnter but will call OnTriggerEnter .它也不会调用OnCollisionEnter但会调用OnTriggerEnter Setting both not as triggers, adding a collider, and giving them Rigidbodies should allow the collision to be detected.将两者都设置为 not 触发器,添加一个碰撞器,并给它们Rigidbodies应该允许检测到碰撞。 You will also need to attach this script to one of the objects that have the collider.您还需要将此脚本附加到具有碰撞器的对象之一。 Are there other components on the objects you are using?您正在使用的对象上是否还有其他组件?

Make sure you are not looking for确保你不是在寻找

void OnTriggerEnter(Collision collision){}

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

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