In the game we are making, we have a feature that causes the player to rotate suddenly for an attack. The problem is that the player’s hitbox is not a circle, but a polygon collider. If you are close to a wall, you can get yourself stuck inside the wall collision. Is it possible to make it so that the player would be allowed to move away from the collision to get unstuck, but not go further in the collision? This would be the simplest fix if it is possible as it is not that big of a deal if the hitbox goes inside the wall a little bit. The problem is that the player gets stuck.
You should not be causing overlap in the first place; this is the primary goal of the physics solver.
Assuming the player Rigidbody2D is Dynamic, set the player to use continuous and use angular-velocity/move-rotation to cause rotation then the solver won’t allow an overlap.
If you (for some reason) cannot do the above and you’re creating a severe overlap then you’ll need to use physics queries to determine a non-overlapped position for the Rigidbody2D.
Looks like I was accidentally use the RigidBody’s rotation
instead or MoveRotation()
If you suddenly change the rotation with MoveRotation()
and collide with another physic object, will the collision cause the physic object to get launches at high speed? I’m assuming MoveRotation()
applies a high angular velocity for one fixed update or something similar to achieve the rotation. At least hitting a wall like this causes the player to take damage, which is based using relative velocity between the colliding objects.
Yes, it’ll rotate really fast to get to that rotation because that’s essentially what you asked it to do. You can set the rotation directly but as you’ve seen, it just means it can teleport into overlap with other colliders.
If you’re setting huge rotations then you’ll get a reaction if it hits the surface, sometimes due to friction. Rotating to that position slower will help (at least over several simulation steps); you can use angular velocity for this.
Also, there’s lots of physics queries to test your environment before you do an action rather than do it and somehow try to figure out how to get out of it.
For instance this: Unity - Scripting API: Rigidbody2D.Overlap
Using this you could check if you’d be overlap at a specific position/rotation.
There’s also Unity - Scripting API: Rigidbody2D.Distance