- Home /
Question by
HonoraryBob · Jun 19, 2015 at 11:02 PM ·
quaternionquaternion.lookrotation
Quaternion.LookRotation problem - always rounded to 90 degree angles
I'm trying to get enemy ships to turn toward the player's ship (and likewise for gun turrets), but the resulting angle is always "snapped" to the nearest cardinal angle - 0, 90, 270 - even though I'm using the same method given in the manual itself and used by other people in the forums. Here's my code.
//calc direction between ship and player
var dir:Vector3 = PlayerPos - ShipPos;
// get rotation the ship needs to swivel toward
var FinalRotation:Quaternion = Quaternion.LookRotation(dir.normalized);
Comment
Answer by zach-r-d · Jun 20, 2015 at 12:35 AM
Instead of using Quaternions, try setting transform.forward:
//calc direction between ship and player
var dir:Vector3 = PlayerPos - ShipPos;
// look in the direction of the player
ShipTransform.forward = dir.normalized;
If the ship can only rotate a certain amount per frame, then Vector3.RotateTowards can limit the rotation:
transform.forward = Vector3.RotateTowards(transform.forward, dir.normalized, MAX_DEGREES_THIS_FRAME * Mathf.Deg2Rad, 1f);