"If player is moving, return true" - not entirely working

Hello, I’ve been trying to get the console to log “true” if the player is moving and “false” when it’s not. I’m running into some potential trouble.

This is my script:

var lastPos: Vector3; 

function Start() {
	lastPos = transform.position;
}

function CharMoved() : boolean {
	var displacement = transform.position - lastPos; 
	lastPos = transform.position;
	return displacement.magnitude > 0.001; 
}

function Update() {
	print(CharMoved());
}

This was taken from this question here.

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?

Here’s an image of the console:

Thank you!

I guess it depends on how fast it’s moving :smiley: If you’ve got high FPS then it could fail that test. You need to make it frame rate independent.

You should probably do this:

function CharMoved() : boolean {
    if(Time.timeScale == 0) return false;
    var displacement = transform.position - lastPos; 
    lastPos = transform.position;
    return displacement.magnitude/Time.deltaTime > 0.001; 
}

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.

Try this:

//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”);
}