I am attempting to write some code to ‘smooth’ the movement moving up a slope using a Linecast and I’m noticing that it behaves really oddly.
High level I am using two points:
Point A = where the character’s foot is. Where they are standing.
Point B = a short distance away in the direction I am moving. Where the collider exists for the sloped floor.
When I am facing the slope this code works perfectly and it hits:
Physics.Linecast(pointA, pointB, out hit)
When I am backing onto the slope (camera is facing AWAY from these points) it fails to recognize the collider at all. I’m using the exact same points for the linecast (regardless of where the player is facing).
When I go back to facing the slope but alternate the points it also strangely fails to hit.
Physics.Linecast(pointB, pointA, out hit)
What is going on with this linecast? Is there some underlying optimization I am unaware of that is just messing up what I’m trying to achieve here?
Aha fixed it. Added a couple more debug statements and it became clear. It tried to do what I asked a couple times but there was another position change occurring conflicting with the rigidbody physics at the same time. After a few repeated attempts to move and pushback from the rigidbody physics the linecast stopped colliding and thought the player and these points were ‘within’ the collider and therefore not colliding BUT the player was actually standing in front of the collider and the rigidbody physics prevented movement forward.
So long story short: if the transform position and rigidbody physics are out of sync. It really messes Linecast up…
Bright side is…I had a bug in the calculated y-increment when I was backing onto the slope so that’s fixed. No more problems backing up into the slope so my code works. (avoided the getting out of sync problem)
I still however do NOT understand why swapping the points affected the linecast. I’m going to make sure that the first point I use in a linecast is always closer to the player for future reference…
My final thought on the sync problem: It looked like the ‘rigidbody’ won the fight for ‘worldspace’ positioning for the player object but the ‘transform’ was still changing that logical position point regardless of where it stood in worldspace (changed the pivot?). Likely a race condition.
Good thing to note though: Once I got in that bad state my player position was unrecoverable. Had to restart.
Depending on what you mean by this, it’s not true. This just sounds like you’re using the Transform position and not the current Rigidbody position for your queries.
The transform pose isn’t necessarily the same as the Rigidbody pose if you’re using interpolation/extrapolation on the Rigidbody. Interpolation is always out of date with it interpolating the Transform pose per-frame from the old Rigidbody pose to the current one. Extrapolation does the same but from the current Rigidbody pose to a predicted Rigidbody pose based upon its current velocity. In these situations, the Transform (and therefore the visible scene) isn’t necessarily hat the physics state is.
If you perform a physics query, you’re querying the physics state; nothing whatsoever to do with Transforms or your visibles. If you’re not using interpolation at all then both will be in-sync.