- Home /
How to change the "front" of my object?
Now I feel like this is a stupid question (i'm trying to learn javascript), but it's really stumped me, so here goes! When I import in a model, I can then rotate it however I like, but the script doesn't treat the visual front (it's a starship) as the front, instead it propels it sideways. Yet rotating it left and right works...
This is the code i'm dissecting:
var life:int;
var thrust:int;
var rotationSpeed:float;
function Update(){
var h=Input.GetAxis("Horizontal");
var v=Input.GetButton("Vertical");
transform.Rotate(Vector3(0,h*Time.deltaTime*rotationSpeed,0));
if(v){
rigidbody.AddForce(transform.forward*Time.deltaTime*thrust);
}
}
Does this have something to do with local or world space? How is the "front"/"sides" of an object set, and how can I change them? I'd appreciate any help you guys can give! Thanks!!!
Please give your question a meaningful title so people know what you're asking about!
Answer by roamcel · Jan 14, 2012 at 08:48 AM
This issue plagues new users.
Try like this:
create a new empty gameobject
put your script on this empty gameobject
drag the model as a child of the empty gameobject, and orient it as it should, with its front following the world z axis (the blue axis)
All rotations that you apply to the parent will propagate to the child, regardless of its weird original rotations (that are 'normally awkward' on imported models)
Ok, I tried that, and the result I got was so ridiculous that i'm now more confused about the script than I was before. When I added the script to the parent it changed how everything worked (I should mention i'm making a 2d game). Now, it's rotating it bottom to top ins$$anonymous$$d of left to right! The top arrow key no longer applys thrust, but if you push the top arrow and then the left or right arrows, it'll apply thrust along the x axis while rotating (in the wrong direction). How long you hold down the top arrow key seems to affect what the left and right keys do...
It's hard for me to explain, because the way it's behaving makes no sense. Somehow adding a gameobject, and a rigid body to that game object radically changed the script...? I didn't change any of the code, fyi. Thanks for the reply! $$anonymous$$aybe you know why it's going bonkers?
Every object can be moved and rotated and scaled based on two different coordinate systems: local and global. What you're experiencing is most certainly related to the confusion that this can cause.
What you need to do is to ensure that your root gameobject has 0, 0 and 0 as rotation angles in the inspector then, if you apply a transform.translate(Vector3.right) to that object, it'll move to the right.
If ins$$anonymous$$d you rotate that object around the Z axis, so that the red arrow points left, if you apply the same translate, the object will move to WORLD right, but it'll move to ITS left!
This is really all there is to it, but it'll drive you mad.
Basically, always figure where the axes are pointing to understand if Vector3.forward, right and up point in the intended direction of movement.
To convert from local to global and viceversa (for example if you need to calculate the forward of a child in world space, you use
Vector3 myworldforward = child.transform.InverseTransformDirection(transform.forward);
For example, if the child's blue axis is pointing 'back' in world coordinates, myworldforward would equal 'Vector3.back'
Oh and I should probably insist on this: FORWARD IS THE BLUE AXIS, RIGHT IS THE RED AXIS, UP IS THE YELLOW AXIS! Orient your objects in such fashion.
I$$anonymous$$PORTANT NOTE: you $$anonymous$$UST make 100% sure that your imported model's scale is 100% 1. ANY scaling in ANY part of it will produce unwanted results in transformation operations.
You can disregard my answer, it hasn't been approved by a mod yet so I can't delete it. But anyways, I used the transform.right and it worked perfectly! But you mentioned that this wasn't the correct thing to do? What's the correct way? Is that where the parent object comes into play because I still dont understand how that helps me because of the rotation problems?
Anyways, thanks for your help, really appreciate it!
heh let's put it like this: if it works, it's the correct way to do it :D Glad you sorted it out
Your answer
![](https://koobas.hobune.stream/wayback/20220613062953im_/https://answers.unity.com/themes/thub/images/avi.jpg)