Removing the physics of a collider

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?

“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).

1 Like

Sounds like you have rigidbodies on them? Just remove those.

1 Like

What would I use to move the player if I’m not using rigidbody? I tried using transform.position, but it ignores collision.

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

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.

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

2 Likes

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.

1 Like

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.

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.