Slime Head and Body both trigger OnCollisionEnter2D when Player lands only on top of Slime Head in Unity 2D game

huangapple go评论68阅读模式
英文:

Slime Head and Body both trigger OnCollisionEnter2D when Player lands only on top of Slime Head in Unity 2D game

问题

在我的Unity 2D游戏中,我有一个玩家对象,附有一个BoxCollider2d和一个Rigidbody2D。

我有一个史莱姆游戏对象,它有一个刚体、身体上的一个碰撞器,并附有一个叫做SlimeBody的脚本,里面有一个用于OnCollisionEnter2D的函数,如果碰撞物体有一个"Player"标签,就会Debug.Log("BODY")。

在这个史莱姆对象中,我有一个名为"史莱姆头"的空子游戏对象,里面有一个盒形碰撞器和一个叫做SlimeHead的脚本。盒形碰撞器当然是覆盖史莱姆头的,并且放置在父对象的BoxCollider2D正上方,它们没有接触。史莱姆头对象里的脚本通过OnCollisionEnter2D函数也会Debug.Log("HEAD"),前提是碰撞物体也有一个叫做Player的标签。

我的问题是,当我跳到史莱姆顶部时,头和身体都会输出Debug.log,即使我确信我只是严格落在史莱姆头上。

如果我从接触史莱姆身体侧面击中它,只会触发身体的debug log。但是当我站在史莱姆头顶并跳跃时,两者都会触发。为什么??

我已经检查过,碰撞器彼此不发生碰撞,而且玩家不可能接触到身体的碰撞器,但两者都会在OnCollisionEnter中运行。

英文:

In my Unity 2d game, I have a Player object which has attached a BoxCollider2d and a rigidbody2d.

I have a slime game object which has a rigidbody, a box collider on its body, and a script attached called SlimeBody which has one function for OnCollisionEnter2D that Debugs.Log("BODY") if the Collision object has a "Player"

In this slime object, I have an empty child game object named "Slime Head" which has a box collider inside and a script called SlimeHead. The box collider is of course put so it covers the slimes head, and is sitting right above the parents boxcollider2d where they are not making contact. The script inside the slime head object Debugs.Log ("HEAD") inside an OnCollisionEnter2d function as well if the collision object has a tag called Player as well.

My issue is, when i jump on top of the slime, both the head and body debug.log, even when I am sure I am landing striclty only on top of the slimes head.

If I hit the slime from the side where I make contact withthe slimes bodys sides, only the Body debug log fires. But when I STAND ontop of the slimes head and jump on it, both fire. Why??

Ive checked and neither colliders collide with eachother, and there is no way that the player is touching the bodys box collider yet both are getting ran inside OnCollisionEnter

答案1

得分: 1

抱歉,不需要提供可重现项目。OnCollisionEnter2D事件也会发送到Rigidbody2D,而且你的物体同时附加了刚体和盒子碰撞器。

为了解决问题,你可以将物体的碰撞器和脚本移到子对象中,像这样:

史莱姆(刚体)
 ├── 头部(碰撞器,脚本)
 └── 身体(碰撞器,脚本)

另一种方法是在你的脚本中比较otherCollider的值:

void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.otherCollider != GetComponent<Collider>())
        return;
}
英文:

Sorry a reproducible project is not needed, The OnCollisionEnter2D event will be sent to Rigidbody2D too, and your body object has both a rigidbody and a box collider attached.

To solve the problem you can move the body collider and the script to a child object like this:

slime (rigidbody)
 ├── head (collider, script)
 └── body (collider, script)

Another method is to compare the otherCollider value in your script:

void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.otherCollider != GetComponent<Collider>())
        return;
}

huangapple
  • 本文由 发表于 2023年5月22日 12:57:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定