- Home /
How to check if a character hasn't changed its position in the last x seconds?
In my game, there is a NPC that follows Waypoints in a sort of maze style game. Its movement is done through the Character Controller function : Move. Sometimes it gets stuck. If move gets called every frame (in the Update())... How would you check every 'x' seconds if the object has changed its position by 'y' much?
Thank you very much!
Answer by DaveA · Apr 02, 2011 at 12:39 AM
Something like:
var lastCheckTime = 0; var lastCheckPos : Vector3; var xSeconds = 3.0; var yMuch = 1.0;
Update() if ((Time.time - lastCheckTime) > xSeconds) { if (npc.transform.pos - lastCheckPos).magnitude < yMuch) DoYourPanic() lastCheckPos = npc.transform.pos; lastCheckTime = Time.time; }
Note this is untested and won't compile but should give you the idea. Also, it will count as 'unmoved' if the npc moves away but then moves back before the next check comes around. To get around that, check more often, and increment a counter of when it hasn't moved. If that count reaches a certain threshold, then panic.
Your answer
Follow this Question
Related Questions
How to forbid an NPC to move diagonally using Character Controller? 1 Answer
Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer
Keeping Character From Walking Through Walls 1 Answer
Small Project, tutorials/help needed 0 Answers
Moving AI away or towards player not working correctly. 1 Answer