Transform.Translate not moving in the direction of specified vector
I'm trying to translate an object in the specific direction of a vector, however using transform.Translate seems to move it in another direction. I'm fairly certain this script works, because when I use a similar method on objects whose motion is controlled by animations it works perfectly. Printing the vector in the debugger also shows it pointing in the right direction, so I am confused as to why it isn't working here. In this instance, however, what usually happens is the object will move in one direction horizontally when pressing W or S, and the other when pressing A and D. Pressing a combination of W or S and A or D will result in forward and backwards motion. I've drawn up an example in case the explanation was hard to understand.
Any help fixing the issue would be appreciated. Here is the code in question:
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
Vector3 stickDirection = new Vector3 (horizontal, 0, vertical);
Vector3 nothing = new Vector3 (0,0,0);
Vector3 cameraDirection = camera.transform.forward;
cameraDirection.y = 0.0f;
Quaternion referentialShift = Quaternion.FromToRotation (Vector3.forward, cameraDirection);
Vector3 moveDirection = referentialShift * stickDirection;
if (moveDirection == nothing) return;
cube.transform.rotation = Quaternion.Lerp (cube.transform.rotation, Quaternion.LookRotation (moveDirection.normalized), Time.deltaTime * 12f);
Vector3 move = transform.forward * stickDirection.sqrMagnitude * moveSpeed;
cube.transform.Translate(move * Time.deltaTime);
Answer by Bunny83 · Oct 30, 2017 at 11:16 AM
Please if you have trouble with any built in method you should first check the documentation.
Moves the transform in the direction and distance of translation.
If relativeTo is left out or set to Space.Self the movement is applied relative to the transform's local axes. (the x, y and z axes shown when selecting the object inside the Scene View.) If relativeTo is Space.World the movement is applied relative to the world coordinate system.
So you pass in a world space direction but it's applied in local space which doesn't make much sense. The resulting direction would be twice the angle relative to the world forward.
So either use
Vector3 move = transform.forward * stickDirection.sqrMagnitude * moveSpeed;
cube.transform.Translate(move * Time.deltaTime, Space.World);
or
Vector3 move = new Vector3(0f, 0f, stickDirection.sqrMagnitude * moveSpeed);
cube.transform.Translate(move * Time.deltaTime);
ps: declaring a variable 7 constant like this "nothing" is just confusing. Just use Vector3.zero which is (0, 0, 0). For more predifined constants see Vector3
Thanks for the help. Sorry for asking such a simple question. Also, thanks for the tip on Vector3.zero! I didnt know that before.
Answer by nihohit1 · Oct 30, 2017 at 09:20 AM
Could this object be the child of a rotated object? In such a case, the child is translated relative to its parent rotation and scale.
The object isn't a child of anything, in this case, so unfortunately that isn't the problem. Thanks for your input though!
Your answer
Follow this Question
Related Questions
How can I make this character move smoothly? 1 Answer
Moving relative to camera 0 Answers
How to make a curve motion change direction fluidly on click 0 Answers
How to move an object on a 2d plane forward, backward, and in a rotation about the mouse position? 0 Answers
How to deal with repetetive code? 2 Answers