简体   繁体   English

Unity OnCollisionEnter具有作为刚体的子代的对撞机

[英]Unity OnCollisionEnter with a collider that is the child of the rigidbody

I am attempting to only have the collision continue when contacting the Player (the parent with the rigidbody), directly; 我试图仅在直接联系播放器(具有刚体的父级)时使碰撞继续; and ignoring collisions with the child (a sword). 并忽略与孩子(剑)的碰撞。 the sword is tagged weapon, and the player with player. 剑被标记为武器,玩家被标记为玩家。

I have searched, and cannot find a sufficient answer (C#) 我已搜索,但找不到足够的答案(C#)

void OnCollisionEnter (Collision col){
    Debug.Log("boop P" + playerNumber);
    if (col.collider.transform.tag == "Player") { 
        -stuff happens-
    }
}

This is driving me crazy and I need sleep, please help. 这让我发疯,我需要睡觉,请帮助。

Edit - I solved it after ages, with a simple thing called ContactPoint.otherCollider 编辑-很久以来,我用一个简单的东西ContactContact.otherCollider解决了它

The problem may be how you're checking for the tag. 问题可能出在您如何检查标签。 I usually grab the gameObject's tag directly like so. 我通常像这样直接获取gameObject的标签。

void OnCollisionEnter(Collision col){
    if (col.gameObject.tag == "Player"){
        //stuff happens
    }
}

Or even the collider's tag. 甚至是对撞机的标签。

(col.tag === "player")

In case anyone else is struggling with this and OP's edit was too vague... 万一其他人为此感到挣扎而OP的编辑太含糊了...

You can check to see the colliders that were hit on the GameObject calling the script using collision.contacts: 您可以检查以查看是否有碰撞对象碰撞到GameObject上,该碰撞对象使用冲撞.contacts调用了脚本:

foreach (ContactPoint c in collision.contacts)
{
        Debug.Log(c.thisCollider.name);
}

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

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