transform.lookat question

so lookat always uses the z/forward? is there a way to make an object lookat target along/using its y or x axis? i found a few bits of code searching but they seemed a bit ‘over engineered’ for what i need…
look rotation seemed viable but i dont need the world up, at least dont think i do… just tryin to get object to keep its bottom aimed at a sphere.
guess instant empty gameobj aimed way i want will have to work for now.

I helped someone else out on this sort of task, check some of my later responses with a demo and source.
It’s hard to do it more simply, because LookAt without a direction could be any orientation in 360 degrees, and calling LookAt can slowly rotate the player arbitrary directions. Without a World Up and a Look Direction you are describing an entire plane and not a specific direction.

http://forum.unity3d.com/threads/72665-Planetary-Gravity

i actually did come across that post searching. but i think ive achieved what i need in a couple lines:

	var hook = new GameObject("OrbitalHook");
	hook.transform.position = thisTransform.position;
	hook.transform.Rotate(Vector3.right * 90);
	thisTransform.parent = hook.transform;
	hook.transform.LookAt(targ.transform);

probably needs some cleaning up maybe some trans cach but in a hurry to run somewhere and coudnt get myself to leave till made it worked haha.

One thing to test out is to draw a line behind the player and make sure moving straight actually equates to moving straight across the sphere.

My first attempts used something similar to that, and the main issue I had was that on the Top hemisphere it worked perfect, but on the bottom half I would always slowly turn towards the very bottom. I can’t explain why, but most of the Unity orientation functions seemed to have a slow lean downwards.

Coulda been fixed, or with the Rotation being hard set like that it may be ok.

I have the same problem. I’m trying to move a cannon tip and make it look at a moving object. I’m doing a simple look at, but at some point when the target object moves from left to right of the cannon base, the Y(up direction) axis is rotated by 180 degrees. The cannon tip is upside down at that moment.

for anyone searching this, i was makin the classic mistake of overengineering (and coding at 5am), when there was code that did what i wanted it to. I just misread how it worked in the script ref.

//gets this transforms -Y axis (down) to look at the target

var direction = targ.position - transform.position;
transform.rotation = Quaternion.FromToRotation(Vector3.up, direct);

In stead of using Transform.LookAt, you could use Quaternion.LookRotation - this lets you specify which up and forward axis you want to use:

transform.rotation = Quaternion.LookRotation (target.transform.position - transform.position, transform.up);

If I ever want to do that I just use transform.Rotate() immediately after the LookAt():

transform.LookAt(target);
transform.Rotate(90.0, 0, 0);
var direction = targ.position - transform.position;
transform.rotation = Quaternion.FromToRotation(Vector3.up, direct);

This works, but only if you don’t try to rotate the forward vector. If you do, the up vector will be reset to the world space up. This is not what I need, so setting the up parameter of the Quaternion.LookRotation works in my case.

The problem with this thing is that Unity always wants to align the Z and Y axes of an object to the world axes when using the Quaternion.FromTo method. If I want to turn a bone around its Z axis but lock the rotation around X and Y, I can’t. If I want to orient the X axis in a direction, then it’s sure the Y will point to up and Z will point forward. I want the local axes to remain the same but only turn around local Z by having X point somewhere.

transform.rotation = Quaternion.FromToRotation(Vector3.right, direct);

This code turns the transform around its Z axis, but in the inspector it’s the Y that changes. This is really weird.

The LookAt only works if the axis you want to orient is Z. Why can’t we have a method that locks rotation around local axes?