Please look at the attached gif video. I really don’t think this is an issue with my code as the horizontal movement is pretty simple, and I can’t see where that would be the issue, but the player is allowed to like slightly go inside a platform which messes up with some of my other functions of the game, is there a way to fix this?
That’s how PhysX physics works. Moving objects penetrate other objects’ colliders, which allows for collision events to be sent as messages, and then the system applies outward impulse forces to “resolve” or extricate the overlap in the next frame(s). There is no code that is able to detect if colliders are touching with zero overlap, they can only detect if they overlap by a non-zero amount.
Yeah, I see what you are saying that makes sense. Is there any way to reduce it or anything to make it better? Or am I stuck with how it is?
Doesn’t Unity use Box2D for it’s 2D physics?
Nonetheless I think the rest of what you said still applies.
you could just make your own physics, the logic is like this:
1 - you know what position your character is going to be on the next frame
2 - would that position result in colliders overlapping?
3 - if yes, treat it as a collision, and deny the movement that would happen in the next frame
There are a couple of things you can do that might somewhat mitigate this.
- Continuous collision mode for the rigid body (easiest thing to try)
- in the project settings physics tab, there a bunch of tweakable. There is also the overall physics simulation rate, which if I am not mistaken is controlled in the Time tab. Note that usually there is a tradeoff between performance, and simulation accuracy.
The code of how you move the character can also influence the result.
The overall scale of things (size, mass, forces) can also change how things look.
Do you have some kind of slow mo mode activate when player jumps, or did you just slow it down to make the problem more easily visible?
Assuming you have artificially slowed it down you can likely also just ignore the problem it’s not like your collider box accurately describes the animated sprite anyway. Arms would be clipping into the obstacle even without penetration and in the “good” state depending on frame there is quite a gap between obstacle and visible part of player (except hand) so without seeing debug collider boxes and at normal speed it would be hard to tell whether there is problem or not.
If slow motion is intended part of gameplay mechanics, I agree with attempts to tweak things for minimizing penetration. Default settings are probably optimized for running at normal speed, it’s not surprising that some issues become apparent when running at slower speed.
It simply looks like you are using discrete collision detection on the Rigidbody2D which is the default as it’s the fastest. That moves then solves any overlaps. Continuous collision detection is much slower but that sweeps the move and ensures it stops at any contact point.
This is a basic physics collision detection option that you’ll find in most physics engines BTW.
This is, of course, assuming you’re allowing the body to move with its velocity and you’re not using a Kinematic Rigidbody2D with queries doing your own detection. You don’t show anything beyond the problem i.e. we don’t see the component uses, their configuration or movement method/script etc.
Honestly I might be addressing the wrong issue, I am trying to make an isGrounded system and I’ve tried ray cast, box overlay, a separate collider but all of them suffer from the same flaw, if you run into a platform (like shown from above) the collider goes inside the platform and it counts as isGrounded and It’s messing up some of my wall climb mechanics because you aren’t supposed to be grounded on the wall obviously
The collider going into another object or not will not solve your issue of the isGrounded bool occurring on your walls. Make sure the Layer or Tag or whatever you are using to check for ground and wall is different between the two, otherwise how will the engine know the difference?
No so like it is a platform that the player is jumping to and I my IsGrounded logic works by just having an overlapbox at the player’s feet. The issue with this is that if the player jumps at a platform and hits the edge/side of the platform the right way the player collider will go inside of the platform resulting in the IsGrounded box going inside of the platform making the game think the player is grounded when in reality they are not.
Here is the line that controls IsGrounded all the variables are assigned in the unity inspector
isGrounded = Physics2D.OverlapBox(new Vector2(transform.position.x + (blueXOffset * transform.localScale.x), transform.position.y + blueYOffset), new Vector2(blueXSize, blueYSize), 0f, groundLayer);
Right, but at the end of your OverlapBox function, your last argument takes a LayerMask type and you put in your variable of groundLayer. So isGrounded will only return true if the collider is overlapped with an object with the layer of Ground or whatever you called it. Check your layer on the Walls you can jump off of and the ground. If they are the same then of course it will be true when the collider hits the wall since it has the same layer as the ground, it doesn’t know the difference at that point