LookAt Limited to One Axis of Rotation

II have an object that is sitting on top of another object.

I would like the object on top to LookAt a point somewhere in the scene, but I would like its pointing to be limited in that it will remain seated perfectly on the object below it.

What I’m looking for essentially is a LookAt that does it’s best to point toward a point in space by rotating only on one axis.

If I understand correctly, and if you are looking to constrain the rotation around one axis (I’ll use the Y axis here) you don’t actually want to look at the second object – you want to look at a point where (for example) the X and Z is of the second object and the Y is of the object that is looking?

In such a case, you could probably multiply the transform position of the object being looked at component-wise by a vector like (1,0,1) using Vector3.Scale. This would get you the position on the ground directly below that object. Then add the Y position of the transform of the object that is looking to Y component of that to get the point where a line straight up/down from one object meets a line straight out from the other.

Maybe this is what you are saying, but just in case …

I would like to have an object that acts like the needle on a compass. Only, imagine that needle, instead of pointing north, pointing at the second object.

Thus, as I move the second object about space on all 3 of its axes, the needle spins on it’s Y axis alone, coming as close to point to the second object as it can, given it’s restriction to single-axis rotation.

You could slightly adapt this.

–Eric

Thanks, Eric.

I tried that, but it doesn’t seem to work.

Why not? Post your c0dez. :slight_smile:

–Eric

Here’s what I’m using to do the pointing right now, based on the example you pointed, no less.

var v : Vector3 = hit.point - playerController.tankTower.transform.position;
v.x = 0;
v.z = 0;
playerController.tankTower.transform.LookAt(hit.point - v);

Thing is, while it works as long as the pointing and point-to objects are level on X-Z, it fails when they are not.

Could it have something to do with world/local coords and such?

Vector3 focus = SecondObject.transform.position;
focus.y = transform.y;
transform.LookAt (focus);

.org

Which does what I was saying, but directly inserts the Y from the looking object instead of scaling and then adding (which, now that I think about it adds a whole step with no benefit). Maybe I just desperately wanted to scale that vector for some reason.

I was suggesting something like:

Vector3 focus = Vector3.Scale(SecondObject.transform.position, Vector3(1,0,1));
// focus is now on the ground directly below SecondObject
focus.y += transform.y;
transform.LookAt(focus);

Oops.

Forgot to include the code.

See edit above.

Thanks, all.

First off, you guys are awesome for trying to help me. I’m sure if I were a bit smarter, I could have used your suggestions.

Unfortunately, I am terminally dumb.

So, I turned to a fellow dummy for help. Namely, a dummy object.

Just in case this might help anyone, I created a dummy object and placed it up a good number of units on the Y-axis of my rotating object. Then, I had it LookAt() the object being pointed at. Then, I used the following code to have the pointing object glean the necessary data from the dummy:

aimingDummy.transform.LookAt(hit.point, playerController.tankBody.transform.TransformDirection (Vector3.up));
playerController.tankTower.transform.localEulerAngles.y = aimingDummy.transform.localEulerAngles.y;

Seems to work just fine.

Whew!