Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Unity 2D game - Player lands on enemy head, triggers both head and body collisions

Discussion in 'Physics' started by oe3827, May 22, 2023.

  1. oe3827


    Jul 22, 2022
    In my Unity 2d game, I have a Player game object with a PlayerController script attached that allows for horizontal movement and a vertical jump.

    My Player has a RigidBody2d and BoxCollider2D, and is being moved and controlled with velocity.

    I have an Enemy game object, which is an evil mushroom, with a RigidBody2d. Inside this mushroom, I have two empty game objects: head and body, with box colliders in their appropriate positions. They both have a script that checks a OnCollisionEnter from the Player and prints to the console “Hit Head” or “Hit Body”

    Since I’m making my player jump with vertical velocity, I change the Players gravity scale to 3 to make the player fall to the ground faster. This works great and as intended

    The issue: Whenever the player jumps and lands on the enemies head, both OnCollisionEnter prints run.

    I closely inspect in the scene view while the game is running, and can see what’s happening is that the Players higher gravity scale when it lands on the enemies head, goes through the head box collider.

    The Players box collider for a fraction of a second when it lands on the head, goes through the heads box collider enough so that it makes contact with the body’s box collider under it. The player then positions itself accordingly so that it stands on the enemies head as intended.

    I have no clue if this is an expected feature or how to go about handling it. I of course would like for different actions to occur when touching the head vs the body of the enemy.

    TLDR: It seems like the players higher gravity is making the players box collider go strongly though the head box collider for a fraction of a second, touching the body’s, and then working as intended.
  2. MelvMay


    Unity Technologies

    May 24, 2013