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

Unity OnCollisionEnter with a collider that is the child of the rigidbody
2020-10-25
  •  译文(汉语)
  •  原文(英语)

我试图仅在直接联系播放器(具有刚体的父级)时使碰撞继续;并忽略与孩子(剑)的碰撞.剑被标记为武器,玩家被标记为玩家.

我已搜索,但找不到足够的答案(C#)

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

这让我发疯,我需要睡觉,请帮助.

编辑-很久以后,我用一个简单的东西ContactContact.otherCollider解决了它

解决过程1

问题可能出在您如何检查标签.我通常会像这样直接获取gameObject的标签.

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

甚至是对撞机的标签.

(col.tag === "player")
解决过程2

万一其他人为此感到挣扎而OP的编辑太含糊了...

您可以检查以查看使用碰撞方法在GameObject上击中的对撞机,它们调用了scripts:contacts:

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

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#)

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

Solutions1

The problem may be how you're checking for the tag. I usually grab the gameObject's tag directly like so.

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

Or even the collider's tag.

(col.tag === "player")
Solutions2

In case anyone else is struggling with this and OP's edit was too vague...

You can check to see the colliders that were hit on the GameObject calling the script using collision.contacts:

foreach (ContactPoint c in collision.contacts)
{
        Debug.Log(c.thisCollider.name);
}
转载于:https://stackoverflow.com/questions/30425270/unity-oncollisionenter-with-a-collider-that-is-the-child-of-the-rigidbody

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入