- Home /
If First Person Controller is moving ?
Hello everyone, I just simply want to check if my character is moving, and I use Unity's First Person Controller, if there was a rigidbody or something I would use
if (rigidbody.velocity != Vector3.zero)
this but there is Character Motor, FPS Input and the other scripts no rigidbody, I don't know how to check if First Person Controller is moving, please help :).
How about caching the position from the last frame and comparing it to 0/1 through Vector3.Distance
?
Answer by aldonaletto · Dec 20, 2011 at 10:18 PM
There exists CharacterController.velocity, but it's not reliable. It's better to keep track of this yourself, like @asafsitner suggested. Add this code to the script where you must know if the player has moved (the script must be attached to the player):
var lastPos: Vector3;
function Start(){ lastPos = transform.position; }
// Any time you call this function, it will return true if the character // has moved 1 milimeter or more since the last time the function was called
function CharMoved(): boolean { var displacement = transform.position - lastPos; lastPos = transform.position; return displacement.magnitude > 0.001; // return true if char moved 1mm }