The problem I have is that when you’re not moving, it seems to be working like normal - Constantly logging “false”. But when you move, it appears to be working as it begins to log “true”, but then it begins logging “false” for every 6 true’s or so.
Any suggestions why it’s doing this, how to avoid/fix it, or another way of doing this?
I don’t think physics calculations are guaranteed to be evaluated between Update() calls. So, maybe Update() can fire multiple times without anything having an updated position, even though the object hasn’t really “stopped”.
Some options to try:
If you’re using a character controller, you can test myController.velocity == 0;
If you’re using a rigid body on the controller, you can test rigidbody.velocity == 0;
Make your test in FixedUpdate(), where I believe physics updates are guaranteed to have occurred between calls.
If your character cannot move without user input (there is no inertia or drift), you can test for user input.
//Checks to see if player is moving and is on ground
if(Mathf.Abs(player.GetComponent(CharacterController).velocity.x) > 1 && player.GetComponent(CharacterController).isGrounded == true)
{
Debug.Log(“you are moving”);
}