Issue with Rigidbody movement one step at a time and gravity
I'm trying to make a game where you can select which action you want to perform one at a time. e.g. the game waits for the user to say which action to do, then executes it and then wait for the next one, and this is done in a co routine which then starts whatever the co routine for the action is. So I based my move routine on the 2D roguelike game, this is what I have:
public IEnumerator MoveRoutine(){
Vector3 end = transform.position + transform.forward;
float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
//While that distance is greater than a very small amount (Epsilon, almost zero):
while(sqrRemainingDistance > float.Epsilon)
{
//Find a new position proportionally closer to the end, based on the moveTime
Vector3 newPostion = Vector3.MoveTowards(m_RigidBody.position, end, m_Speed * Time.deltaTime);
m_RigidBody.MovePosition (newPostion);
//Recalculate the remaining distance after moving.
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
//Return and loop until sqrRemainingDistance is close enough to zero to end the function
yield return null;
}
}
Now the issue comes with the jumping and gravity part. Since there are "holes" in the field where the player can fall off I want to have gravity on the player Rigidbody. However, if I activate this then the movement routine gets stuck in an infinite loop, because for some reason the value of Y that the player's rigidbody has goes to like 0.750000004 instead of the original 0.75.
Any thoughts on this? What can I make different? I have been reading that the physics engine doesn't like you to move stuff with MovePosition, but these are comments saying that it should be done in FixedUpdate instead of Update, but I'm doing it in a coroutine.
Thanks in advance guys :)
The 0.75004 value is normal. When you let physics move you around, like you are with gravity, you won't get the exact numbers you'd think. There are various tolerance values -- like if you stack two 1x1x1 cubes, the top cube won't be exactly 1 above the bottom one.
float.epsilon is for proper math, measuring one true rounding error. Physics isn't rounding -- it's just saying that being inside another object by only 0.01 is close enough.
$$anonymous$$aybe try some tests, in a new scene of letting an object move and fall, watch the numbers, and play with settings. That should give a feel for it.
Thanks. You actually gave me a cool idea. So for my Epsilon comparison I created 2 new "current" and "end" vector3d but with 0 in Y, so that I'm always checking on the movement in my X and Z axis. It works, it actually finishes the loop now. However, now while the player moves if there is a hole underneath it looks like it floats until it reaches the desired position in X or Z.
Do you know by any chance if there is a way for me to "move" an object from A to B on a single axis and at the same time let gravity do it's thing? I think that summarises my original question a lot better.
Yeah, consider using your own epsilon. Just some negligibly small, close-enough value.
As for the move-position and coroutine thing, you can try yield return new WaitForFixedUpdate() (ins$$anonymous$$d of yield return null) to time things correctly.
(replying to owen comment to prevent the unity comment tree)