Hi! I don’t understand the difference really.
Translate() actually moves something. TransformDirection() just takes a vector, and puts it in world space. For example, if your character is facing the Z axis, and then rotates 90 degrees to the left, TransformDirection(Vector3.right) would yield the same thing as Vector3.forward.
Transform.right, Transform.up, and Transform.forward are shortcuts for TransformDirection(Vector3.right), TransformDirection(Vector3.up), and TransformDirection(Vector3.forward), respectively.
So, its local coordinates transformed into world ones.
It really blows my mind.
Like, here is a part of moving FPSWalker script from the Standart Assets/Scripts
//...
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
//...
var flags = controller.Move(moveDirection * Time.deltaTime);
//...
If all this was just for moving, why is there such a hassle?
Can’t it as simple as:
transform.Translate(0,0,Input.GetAxis("Vertical") * Time.deltaTime *= moveSpeed);
Of course more practice will help me, but I’m just trying to fasten this process.
No. A Character Controller is akin to a rigidbody. I have to assume that code is in FixedUpdate. If you just use Translate(), it will go through colliders.
I think I get it. If I need to work with CharacterController in order to move it around I need world coordinates for GetComponent(CharacterController).Move(); function. And TransformDirection(); creates them.
Yep. Check out what’s in the documentation for further examples.
Thanks for your helps Jessy. I really appreciate it.
Then again, if you leave the following line out…
moveDirection = transform.TransformDirection(moveDirection);
… it would also work.
Why would moveDirection be ‘wrong’ if you would not use TransformDirection?
Actually it doesn’t…give it a try. Without TransformDirection, it doesn’t go in the correct direction.
–Eric
As long as whatever you’re moving is perfectly aligned with the world axes, you don’t need to bother with TransformDirection().
Ahh right! Thanks!