Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

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

  1. ofloo23

    ofloo23

    Joined:
    Jul 22, 2022
    Posts:
    2
    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

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,256