- Home /
Rotation towards target gets faster when target is under or close to source object
Hi, I tried multiple solutions to rotate towards a target - but the problem remains:
If the target is underneath (or veeery close) the source object the source object spins / rotates really fast instead moving with a constant speed. (In most solutions you can define the speed or steps * Time but still the rotation gets faster in this special case)
the source object should always rotate with a constant speed.
My setup: my source object follows the target object and rotate towards it. However the target object can sometimes be slower so it ends up at the same position as the source object before it move on to the next waypoint. At this point my source object starts rotating too fast . In all other cases where the target object is farther away it works out fine. I think its an angle / math problem but I dont know how to approach it. Thanks!
Answer by keimax1981 · Jan 08, 2019 at 12:02 PM
Answering myself here:
This works:
Quaternion limitedRotation = new Quaternion(0F, 0F, 0F, 0F);
Quaternion rotation = Quaternion.LookRotation(target.position - this.transform.position);
//lock rotation, yaw only
rotation.eulerAngles = new Vector3(0, rotation.eulerAngles.y, 0);
//rotates the Ship to Reticle with fixed speed
limitedRotation = Quaternion.RotateTowards(this.transform.rotation, rotation, 15 * Time.deltaTime);
this.transform.rotation = limitedRotation;
its from: https://forum.unity.com/threads/rotating-at-a-constant-speed.165458/