- Home /
"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!
How are you handling the player's movement...? Are you handling user-input with FixedUpdate? If so, you should probably use regular Update, in s$$anonymous$$d, since FixedUpdate isn't necessarily called every frame, and can render user input quite buggy.
Answer by whydoidoit · Apr 28, 2013 at 10:38 PM
I guess it depends on how fast it's moving :D 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;
}
Answer by s_guy · Apr 29, 2013 at 01:01 AM
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.
Answer by eweeparker · Aug 03, 2014 at 08:46 PM
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"); }
Your answer
Follow this Question
Related Questions
Mouse as a throttle 1 Answer
Character controls broken due to mouse looking script 1 Answer
space moving physics 1 Answer
Spawn player if player dead 2 Answers
Throw the Player from the Enemy away 1 Answer