- Home /
Changing the gameobject xyz-axis
Hi,
is it possible to change the xyz-axis of a gameobject?
Say by instance there is a gameobject with the z-axis facing backwards, and you want the z-axis to face forward. Because maybe you would like to create a script with a LookAt()
function so it's facing the right direction.
I know you can parent the gameobject in an empty gameobject with the correct orientation. BUT, this adds another problem. That is, whenever you want to destroy the gameobject while colliding with something else, it only destroys the colliding gameobject (and not the empty gameobject, because you don't want to add a collider to the empty gameobject for several reasons). And yeah, I know there are several other solutions to solve these problems (adding tags etc.), but this is really a time-consuming method. Especially when you have lot's of different gameobject that has to do the same thing.
So it would be a lot easier if there was a way to change the xyz-axis in an orientation with respect to the gameobject without adding the gameobject into an empty gameobject.
Is there any way to do this?
As a comment on your empty game object; you can always access the transform.parent, and destroy that. It's maybe another 1 or 2 lines of code tops. Corrective nesting is probably going to be your easiest option, if you can't re-save the asset with a corrected axis.
Answer by Eric5h5 · Apr 25, 2011 at 11:35 AM
No, not really. You should always just create your objects so the z-axis faces forward in the first place, saves lots of headaches.
Unity could save us all a lot of pain by starting 2D projects with an orthographic camera looking down ins$$anonymous$$d of forward.
Answer by Joshua · Apr 25, 2011 at 12:12 PM
Instead of changing it it is much easier to just have an empty GameObject.
First: Child this empty gameobject to your original gameobject Then: Reset the transform of your empty gameobject so it centers. Then: Un-child the empty gameobject Then: Rotate the empty gameobject to face the way you want Then: Child the original gameobject to the empty gameobject.
Now you can use this empty gameobject as the original gameobject with the axes fixed.
Answer by Jean-Fabre · Apr 25, 2011 at 11:35 AM
Hi,
The only alternative given your requirements is to rotate the mesh vertices themselves, that is: Iterate each vertices of your mesh, and rotate them. In Max, this is actually how it's done internally when you affect the pivot.
I just can't find the script that did that, I am sure someone already build an editor or even run time tool for that, but just can't find it, will edit when I will :)
Bye,
Jean