- Home /
How to move in a direction rather than in axis using joysticks?
Hi there, I am very new to Unity and I'm struggling on my current project. I'm basically creating a first person exploration game in which I want to use my controller (gamepad as I've named it) to control my player. I have looked at a variety of resources and created my script which is almost working. My left joystick does make the player walk, A does jump and the right joystick does make him look around.
However an issue I'm having is that my player doesn't walk in the direction my camera is facing. Instead he walks along the x and z axis.
This is the script I've created:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
private Vector3 movementVector;
private float moveHorizontal;
private float moveVertical;
private CharacterController characterController;
private float movementSpeed = 8;
private float jumpPower = 15;
private float gravity = 40;
public float turnSpeed = 50f;
private Vector3 moveDirection;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
Vector3 dir = Vector3.zero;
Vector3 movement = new Vector3(dir.x, 0, dir.y);
movementVector.z = Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed;
movementVector.x = Input.GetAxis("Gamepad_LeftJoystickY") * movementSpeed;
if (characterController.isGrounded)
{
movementVector.y = 0;
if (Input.GetButtonDown("Gamepad_A"))
{
movementVector.y = jumpPower;
}
}
var y = Input.GetAxis("Gamepad_RightJoystickX");
transform.Translate(dir * movementSpeed * Time.deltaTime, Space.World);
transform.Rotate(0, y, 0);
// Right Stick is mapped to HorizontalR and VerticalR
movementVector.y -= gravity * Time.deltaTime;
characterController.Move(movementVector * Time.deltaTime);
}
}
Sorry if this does look a bit of a mess, I'm new to coding. Please could someone help me fix this.
Answer by Harinezumi · Jun 01, 2018 at 11:18 AM
What you are trying to achieve is camera relative movement. Basically you need to get a reference to your viewing camera (can be Camera.main
if that is the one), and multiply movementVector.z
with that camera's transform.forward
and movementVector.x
with transform.right
. Check out this forum post for a more details, or this answer (although the formatting is a mess).
Note that with this change transform.Rotate()
will not turn your character in the desired direction: you will have to get the angle at which you point the right joystick ( Mathf.Atan2()
is very useful for this), and then rotate relative to the camera's transform.forward
.
BTW, I noticed that you move both by Transform.Translate()
and CharacterController.Move()
. Don't do this, you will get conflicting results, because transform.Translate()
ignores physics, while CharacterController
uses it. This didn't cause you problems, because dir
is zero-vector, but it could have. Just remove the variable dir
and transform.Translate()
call, and directly set movement
to Vector3.zero
, and you should be fine.
Ok I'm trying it out, how do I multiply the movementVector with the camera's transform.forward because it seems to come up with an error when I try it like this.
movementVector.z * transform.forward = Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed;
Seems that the line comes up as an error
You cannot have an instruction on the left side of =
. Check out the linked forum post for working code example, but it should be something like this:
movementVector = Camera.main.transform.forward * Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed;
Note that you might want to project the camera's forward vector onto the xz plane by doing this:
Vector3 cameraForward = Camera.main.transform.forward;
Vector3 projectedForward = new Vecto3(cameraForward.x, 0, cameraForward.z).normalize;
Unfortunately, the above will give you an invalid vector if your camera is looking directly up or down (forward is (0, 1, 0) or (0, -1, 0)). But you should try to avoid that situation anyway.
Forum post:
https://forum.unity.com/threads/moving-character-relative-to-camera.383086/
Camera.main.transform.forward * Input.GetAxis("Gamepad_LeftJoystickX") * movementSpeed
This section has been errored, stating "Cannot implicitly convert type 'UnityEngine.Vector3' to 'float'. I've made the changes and this is the only error that's come up. Could I missing something that should of been stated near the beginning of my script.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163846im_/https://answers.unity.com/themes/thub/images/avi.jpg)