- Home /
Question by
jtsmith1287 · Feb 20, 2015 at 06:56 AM ·
movementtransformvector3
Prevent jittering of object movement
Below is a snippet of my AI movement code. Everything works 100% how I want it, except that when the AI's target (a player) is standing still and the AI is at the threshold for the two distances, it basically bounces back and forth, causing this jittery motion. I'm not sure how to go about fixing this cleanly. I've tried nesting the distance check code, the movement code, and both chunks, into an if statement with some kind of check of "You're at your destination now, so see if you need to move again" or something like that, but for obvious reasons those approaches do nothing. I'm having a brain fart I think.
void Move() {
float distance = Vector3.Distance(transform.position, BaseShip.Target.position);
if (distance > Distances[State]) {
// Move towards target
Destination = BaseShip.Target.position;
} else if (distance - 1 < Distances[State]) {
// Move directly away from target
Destination = transform.position + (transform.position - BaseShip.Target.position);
}
transform.position = Vector3.MoveTowards(
transform.position,
Destination,
Time.deltaTime * BaseShip.Speed);
}
For the sake of the exmaple just assume Distances[State] is 10f or something. Doesn't matter.
Comment