- Home /
Using LookAt method to get object X rotation to face another object
I have a object that i want to rotate only on its X axis to "look at" another object. But i think i have imported it incorrectly. It is a ship model that i want to rotate to point to the object that its traveling to. This is what i basically do: this.gameObject.transform.LookAt(this.destination.transform.position); But instead of the source object to "look at" the destination, the object's X rotation is way off.
Help please :)
Answer by robertbu · Mar 30, 2013 at 10:20 PM
It is most likely the model. Place a small sphere at the origin. Then take your model and set the position and rotation to (0,0,0). The sphere should be inside your ship at the center of its rotation, and the front of your ship should be facing positive 'z'. If either of these two are off, then you need to fix things. The best way is to fix it in the modeling program, but you can fix it by placing an empty game object at the origin, placing the ship at the origin with the front facing positive 'z', then making the ship a child of the empty game object. Your rotation/position code is then applied to the empty game object. This does not work for all situations of offset position/rotation, but it can work for many of them.
I've tried adding an answer to your suggestion, but it seems lost ... anyway. The sphere is in the perfect center of the object.
The object with 000 rotation looks like this:
But when i use the lookat code the rotation is way off:
Since what i am doing is a 2.5d project, i believe only rotating on the Z axis should be enough (and considering the destination and the rotating object share the same Z coords), but for some reason it is not taking under consideration the Z axis at all.
Forward is always positive 'z'. So if your camera is in the default position (looking at forward 'z'), then it is the side of your ship we do not see which is forward (which is exactly what you see in the this photo). As mentioned above, you can "fix" this by:
placing an empty game object at the pivot of the ship (i.e. where the sphere was placed).
Rotating you ship so the nose is facing positive 'z' with the top of your shop up (facing positive 'y').
$$anonymous$$ake your ship a child of the empty game object by dragging and dropping the ship on the empty game object.
Place your code with the LookAt() or LookRoataion() on the empty game object.
This seems to work, although a bit hacky. Since i have the original 3ds file, can you recommend a read that covers how to fix this issue in the modeling program?
I think i have found the solution, basically i changed the pivot points of the object in max to be the way you suggested, i.e. the nose to face positive Z and the "up" to face positive Y. Then when exporting to fbx select "Y-up" and that's it!
Thank you Robert!
Your answer
Follow this Question
Related Questions
LookAt Translation Not Sticking 1 Answer
Lock rotation axis? 4 Answers
Joint Transform Rotation in Unity? 0 Answers
transform.LookAt issue 1 Answer
How do I get GameObjects to look at me? 2 Answers