Turn - Based Strategy Game : Camera Controller - movement

I implemented the camera system and it is working as shown in the course. However the movement does not work as I would imagine it to work as a user. When rotating the camera the directions change. 90 degree rotation, w & s switch with a&d in movement directions, 180 degree w and s switch with each other as does a and d.

So if the player presses W he expects the camera to move in the forward direction of what he sees, but its now moving right instead.

Anybody can help with this, I failed so far to solve it.

Many thanks

Andreas

For the camera to move in the expected direction regardless of its rotation you need to multiply the player input by the camera’s transform.forward for forward/back and transform.right for right/left.

The way it’s done in the course:

        float moveSpeed = 10f;

        Vector3 moveVector = transform.forward * inputMoveDir.z + transform.right * inputMoveDir.x;
        transform.position += moveVector * moveSpeed * Time.deltaTime;

Thanks for your answer, but I might have failed to explain myself well enough. The camera is doing fine and works, got that in the code above. The issue is how the w,s,a and d keys work.

At the start w move you forward and a left etc. all fine, but as soon as I apply a rotation with the q / e key things go wrong. After a 90 degree rotation a moves you forward and w move you right, another 90 degree rotation and w moves you backward and a moves you left, another 90 degree rotation w moves you left and a moves you backward.

The player would expect to always move forward no matter what the camera rotation is. Any advise of how to tackle that please?

Thanks for your support

Andreas

If you are using the above code then your camera shouldn’t be behaving the way you’re describing, that’s the purpose of using transform.forward and transform.right. Can you please share your code for the camera’s movement and rotation?

Hi, Thanks again for your reply. Here the code for the camera:

public class CameraController : MonoBehaviour
{

#region Variable Declarations

[SerializeField] float moveSpeed = 10f;
[SerializeField] float rotationSpeed = 100f;

#endregion

#region Unity Methods


// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

    Vector3 inputMoveDirection = new Vector3(0,0,0);

    if (Input.GetKey(KeyCode.W))
    {

        inputMoveDirection.z = +1f;
    
    }

    if (Input.GetKey(KeyCode.S))
    {

        inputMoveDirection.z = -1f;

    }

    if (Input.GetKey(KeyCode.D))
    {

        inputMoveDirection.x = +1f;

    }

    if (Input.GetKey(KeyCode.A))
    {

        inputMoveDirection.x = -1f;

    }

    Vector3 moveDirection = inputMoveDirection.z * Vector3.forward + inputMoveDirection.x * Vector3.right;

    transform.position += moveDirection * moveSpeed * Time.deltaTime;

    Vector3 rotationVector = new Vector3(0,0,0);

    if (Input.GetKey(KeyCode.Q))
    {

        rotationVector.y = +1f;

    }

    if (Input.GetKey(KeyCode.E))
    {

        rotationVector.y = -1f;

    }

    transform.eulerAngles += rotationVector * rotationSpeed * Time.deltaTime;

}

#endregion

#region Class Methods


#endregion

}

Regards,

Andreas

I can’t find any problems with the code myself! Perhaps someone else will spot something but the problem may be elsewhere.

Double check that your Cinemachine’s virtual camera settings have the CameraController set as both the LookAt and Follow target.

Annotation 2024-02-20 013334

Also, just in case, the CameraController script shouldn’t be attached to the camera or to the virtual camera, it should be attached to an otherwise empty Game Object.

Dear Satinel,

Thanks for your efforts. I worked it out and replacing the transform.position line with

transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.Self);

done the trick. Now it works. The issue was that the camera rotation brings it to local space and the movedirection is according to world space. I could have calculated the camera affect manually but opted to use the translate and the parameter space.self, which does the same thing.

Regards,

Andreas

1 Like

I’m glad you were able to find a solution! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms