- Home /
How can I move character controller in one direction
Hi guys. I want to move my character controller in one direction, but I can't.
I can move my character in all axis with this code;
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
public Camera cam;
Vector3 scale = new Vector3(1, 0, 1);
Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized;
Vector3 camRight = Vector3.Scale(cam.transform.right, scale).normalized;
Vector3 vertical = camForward * vAxis;
Vector3 horizontal = camRight * hAxis;
Vector3 move = (vertical + horizontal);
I can move my character in only X axis with this code;
Vector3 scale = new Vector3(1, 0, 0); // I set X axis to 1, others to 0. So character controller is moving in only X axis.
Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized;
Vector3 camRight = Vector3.Scale(cam.transform.right, scale).normalized;
Vector3 vertical = camForward * vAxis;
Vector3 horizontal = camRight * hAxis;
Vector3 move = (vertical + horizontal);
I can move my character in only Z axis with this code;
Vector3 scale = new Vector3(0, 0, 1); // I set Z axis to 1, others to 0. So character controller is moving in only Z axis.
Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized;
Vector3 camRight = Vector3.Scale(cam.transform.right, scale).normalized;
Vector3 vertical = camForward * vAxis;
Vector3 horizontal = camRight * hAxis;
Vector3 move = (vertical + horizontal);
But I can't move character in only a direction, b direction or c direction. (Look at the image, you can see, a b c directions) How can I move my character in only a direction, b direction or c direction? Thank you very much guys...
First, some optimizations: Vector3(1, 0, 0)
is the same as Vector3.right
, Vector3(0, 1, 0)
is the same as Vector3.up
, and Vector3(0, 0, 1)
is the same as Vector3.forward
. These are NOT the same as transform.forward/up/right
, because those depend on the rotation of the transform
.
The code that uses Vector3.Scale()
can also be simplified to just excluding the coordinate you don't want to use, e.g. if Vector3 scale = new Vector3(1, 0, 0);
then ins$$anonymous$$d of Vector3 camForward = Vector3.Scale(cam.transform.forward, scale).normalized;
you can write Vector3 camForward = new Vector3(cam.transform.forward, 0, 0).normalized;
. This saves you 2 unnecessary multiplications. Unless, of course, scale
will be very variable, then the original code is more general, and better.
Now for your actual question: what are the directions a
, b
, and c
? Are they at 45 degrees between the axes? Anyway, the code should work if you set scale
to the direction (axis) along which you want to move and normalize it (if the coordinates are not equal). What are you seeing that makes you say it doesn't work?
Your answer
Follow this Question
Related Questions
"Fake" Vector3.forward? 3 Answers
Get which way the player is moving from character controller.move 2 Answers
Fire Knockback, Vectors, Directions 1 Answer
How to find the direction to translate an object, to move towards a point, regardless of rotation? 1 Answer
Calculate direction of travel. 3 Answers