- Home /
 
Why does this script work correctly?
Hello,
Here is the script I did after some testing, but I don't get why it works.
function FixedUpdate() {
 
                transform.Rotate(Vector3.up  * Input.GetAxis("Horizontal"));
 if(Input.GetAxis("Vertical") > 0)
 {
 transform.Translate(Vector3.forward * ( Input.GetAxis("Vertical")));
 //rigidbody.AddRelativeForce(Vector3.forward * (1000000 * Input.GetAxis("Vertical")));
 }
  
               } 
I have a game object which implement a rigid body component and the script above, and which contains my ship's object (mesh + collider etc).
The script works the same way with the commented part instead of the line right above.
My question is : why my translation works correctly since I never transform the direction it follows according to my rotation?
I mean, I usually did(or saw) things like
myDirection = transform.TransformDirection(myDirection);
 
                
                
               to take care of the rotation, but this script works perfectly without that.
Can someone explain me why?
Thanks.
ps : I'm new to unity, so please don't be rude ;)
Answer by Bunny83 · Apr 11, 2011 at 10:04 PM
If you take a look at the scripting reference on Transform.Translate you will find out that the given vector is treated as local direction by default (relativeTo = Space.Self). That's why your direction refers to the local forward (z-axis)
Your answer
 
             Follow this Question
Related Questions
Translating measurement in rotation speed and game object size to non-technical staff 0 Answers
is there any possible to use two transform.rotate function for same gameobject?? 2 Answers
Moving objects together without parenting 5 Answers
Aligning a transform according to two different positions and directions 1 Answer
Transform.Rotate not stopping? 1 Answer