Very Simple Space Ship Problem (LookDirection?)

Just mucking around with unity trying to aim a space ship:

var pointDistance : float = 50.0;
var turnSpeed : float = 3.0;

function LateUpdate(){

 var targetPos : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y,pointDistance));

var rotation = Quaternion.LookRotation(targetPos - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
}

It works pretty good, except of course when you start pointing towards world up or world down where it gets a bit spastic.

Just was wondering how you avoid this problem - so that the ship can do loop the loops etc.

I’m thinking that it’s a problem with Quaternion.LookRotation and its world direction. I’m hoping there is a quicker way of solving this than writing my own lookrotation algorithm.

Thanks for your time.

A quick solution might be to submit the ship’s current up vector as the ‘up’ argument to LookRotation().

If you’d rather try a different method though, alternatives include using Transform.Rotation() to apply local incremental rotations, or using Quaternion.SetFromToRotation() to create a rotation that will rotate the ship’s current forward vector onto the target forward vector over the shortest arc.

Thanks, that works great for now! I’ll look into other options as I refine the system.