LookAt With Different Axes

I want to rotate a transform so that the x-axis is always pointing towards a target, like LookAt except with the local +x-axis instead of the local +z. I must be missing obvious, though, because I can’t make it work. I could use some help.

This appears to work, even though I’m not sure why I have to apply the extra rotation.

var direction = target.position - transform.position;
transform.rotation = Quaternion.LookRotation(Vector3.forward, direction);
transform.Rotate(0,0,90);

Aha! Thanks…! :smile:

Small change in SmoothLookAt that lets you decide what axis to use as look at vector:

var target : Transform;
var damping = 6.0;
var smooth = true;

var lookVector = Vector3.up;

@script AddComponentMenu("Camera-Control/Smooth Look At 2")

function LateUpdate () {
	if (target) {
		var rotation = Quaternion.FromToRotation(lookVector,target.position - transform.position);
		if (smooth)
		{
			// Look at and dampen the rotation
			transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
		}
		else
		{
			// Just lookat
		    transform.rotation = rotation;
		}
	}
}

function Start () {
	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}