Physics2D.Linecast unreliable with Rigidbody2D interpolation? What to do
Hello I am creating a platform game and I am using Physics2D.Linecast to detect whether my character is on the ground and thereby able to jump. I am currently trying to implement platforms that fall down when you land on them. My problem is that sometimes when the character lands on one of the platforms I can see that the onGround bool is false. This confuses me a lot and I tried to use Debug.Drawline to draw the same line that Physics2D.Linecast is using and when I get those frames where onGround is false I can see that the line is in fact overlapping the collider on the platform and the onGround variable should therefore be true.
Both my character and the falling platform have rigidBody2D components attached to them with the interpolation setting turned on. I tried to test it without interpolation and it seems to work but it looks jittery and horrible. I am looking for some kind of explanation as to why this is happening and if anyone can come up with a way or some tip on how to make the Linecasting reliable it would be awesome. Thank you in advance.
Answer by MelvMay · May 23, 2016 at 07:21 AM
The physics queries are 'reliable'. It seems that you don't understand what Interpolation actually is.
The physics system movement is based upon your fixed-update and therefore moves the physics bodies around in coarser steps and not per-frame. Interpolation (not extrapolation) moves the Transform-only per-frame from the last body position to the current body position to give smoother movement for things that render/depend on the Transform such as sprites etc. The physics body isn't affected by interpolation, it's the basis for the Transform updates only.
When you perform casts (etc), you are performing it against the body/collider positions and not against the Transform/GameObject/Sprite positions i.e. it has nothing to do with the Transform position. You can output this if you look at the Rigidbody2D.position as compared to the Transform.position.
Thank you for clearing that up for me. I have figured out a different way to make it work but your comment has made me realise a possible explanation as to why I couldn't make it work with the linecasting. With what you just cleared for me in $$anonymous$$d I think the problem was that the origin point of the linecasting on my character gameObject was done by using the gameObject.transform.position where I should probably have used the rigidbody2D.position ins$$anonymous$$d. This could explain why I was having no trouble without any interpolation.
Your answer
Follow this Question
Related Questions
Unmoving Colliders2D not triggering OnEnterTrigger 1 Answer
Different rigidbody2D not working together? 2 Answers
How to make instantiated rigidbodies continue moving the in the same direction as destroyed object? 1 Answer
RiigidBody2D.AddForce() isnt working at all. 0 Answers
Physics2D, impersonate old RigidBody2D "Freeze Position" How? 1 Answer