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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Removing the physics of a collider

Discussion in 'Scripting' started by jeffreyshoe1, Dec 9, 2022.

  1. jeffreyshoe1

    jeffreyshoe1

    Joined:
    Oct 31, 2022
    Posts:
    7
    I'm using box colliders as boundaries for my simple 2D game so the player character does not move out of screen view. However, when the player collides into a boundary it starts rotating in different directions, possibly due to physics causing it. So is there a way to remove the physics part of the collider, but keep the rest?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    "The physics part of the collider" is the Rigidbody. Delete the Rigidbody if you don't want it to act that way (Or alternatively, make the Rigidbody's body type Static).
     
    AnimalMan likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Sounds like you have rigidbodies on them? Just remove those.
     
    AnimalMan likes this.
  4. jeffreyshoe1

    jeffreyshoe1

    Joined:
    Oct 31, 2022
    Posts:
    7
    What would I use to move the player if I'm not using rigidbody? I tried using transform.position, but it ignores collision.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    You need to read-up on what the three body-types are:

    • Static: Doesn't react to collisions/forces/gravity etc. Should not be moved. Typically used for level/ground stuff that never moveds.
    • Kinematic: Doesn't react to collisions/forces/gravity etc. Can be moved using the Rigidbody2D API. Typically used for character controllers or things that need explicit movement control.
    • Dynamic: Reacts to all collisions/forces/gravity etc. Can be moved using the Rigidbody2D API. Used for all dynamic physical movement elements of your project. This is the default when you add a Rigidbody2D to a GameObject.
    You can change the body-type in the Rigidbody2D component in the inspector.

    If you don't select "Static" then all colliders attached to it are implciitly Static (see above).

    For level stuff that should never ever move (such as your "boundary") then you don't need to add a Rigidbody2D to make it implicitly Static but it's the same as adding a Rigidbody2D and selecting Static.

    Note: Never use the Transform to change the position or rotation.

    Were you not asking about the "boundary" and not the player? The suggestion above was to remove any Rigidbody2D from the "boundary", not the player. If you read the above, you'd then understand that this makes them Static (non-moving/reacting).
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    To add, if it's the player that is rotating then you can stop that by going to the Rigidbody2D component on the player in the Inspector, opening up "Constraints" and selecting "Rotation". This will stop it rotating due to collisions/forces etc.
     
  7. jeffreyshoe1

    jeffreyshoe1

    Joined:
    Oct 31, 2022
    Posts:
    7
    I was referring to the player, my mistake if I wasn't clear enough. But checking the "freeze rotation z" on the player's rigidbody was the solution I was looking for. Thanks
     
    spiney199 and MelvMay like this.
  8. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    For some reason this solution seemed to have been glossed over, but if you check the "Is Trigger" box on the boundary Colliders and change your code to use OnTriggerEnter instead, you won't have to worry about forces from the collision affecting your object, as it checks for a collision, but doesn't perform any forces on it.
     
  9. jeffreyshoe1

    jeffreyshoe1

    Joined:
    Oct 31, 2022
    Posts:
    7
    I did try this and coded it to stop the player's movement using OnTriggerEnter2D method, but the player can just move out of screen after the initial stop. There probably could've been another way to circumvent this, but that was all I could do with my limited coding skills.
     
  10. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Oh, I see, I misunderstood what you meant in the original post. I assumed you meant like a killbox, which the Trigger colliders would work with. Your solution to freeze the z rotation is a good solution.