How to LookAt a position along local axis only?

Hi, I’ve read a few posts asking the same questions and tried the code but it doesn’t do what I want.

I’ll just explain what I want first.

I have a sphere and I want to place turrets on the surface of the sphere (say, a planet). So, I instantiate a turret on a facet of the sphere using the facet normal to orientate it. I want this normal to be my Up axis (local Y of the turret). Then, I want the turret to face objects that are orbiting the sphere but only by rotating along its local Y axis only.

I’ve tried LookAt but this won’t let me rotate around only the local Y. I’m guessing I need to use Quaternion rotation but I’m not sure where to start.

A hint, explanation, pseudocode or javascript would be appreciated.

Thanks

There are some tricks in UnityAnswers for an object on regular tilted ground, always <90 degrees (because Terrain can’t flip upside down.) But I think they’d break for upside-down towers.

The usual solution for a “flat” lookAt is to flatten the position before looking – don’t look at the bird, look at where it would be on the ground. But that only works if you want to spin around the real Y axis. To do it in your own personal Y, use the local-coordinate trick. Bring it to your point of view, flatten it there, then bring it back to real coords. Not specifically tested:

Vector3 localPos = transform.InverseTransformDirection(targPos-transform.position);

localPos is now the numbers if you were the camera at (0,0,0). For example, localPos.x>0 means the target is to your right. Next flatten it:

localPos.y = 0;

This flattens it out, but the numbers are still from your point of view. If we watched it move, you’d see it go straight down, but anyone else would see it move diagonally to your personal “ground plane.” Finally bring it back to real xyz:

Vector3 lookPos = transform.position + transform.TransformDirection(localPos);

Then use your facet normal for the UP in lookAt.

Hi, thanks for that, that is what I was thinking.

I tried it a few times but couldn’t get it right but I’ll try your method.

I have similar problem, and i think i will help (my problem, when i writing this text, isn’t solved, but it helps to rotate without using some angle)