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.
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);
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: