0
\$\begingroup\$

I am trying to implement camera movement like it is done in Guild Wars 2. Scenario is when A and D keys are pressed, the player rotates right / left and the camera follows and stays behind the character / player like this (Starts at 23 seconds)
https://www.youtube.com/watch?v=XkHJ2uSrLjE

I can get the basic working fine, and I did it this way

private void KeysRotateCameraAndPlayer()
{
    float keysOrbitingSpeed = 120;
    this.player = GameObject.FindWithTag("Player").transform;

    if (Input.GetKey(KeyCode.A))
    {
        Debug.Log("Rotate Left");
        CenterPoint.eulerAngles -= new Vector3(0, 360 + Time.deltaTime * keysOrbitingSpeed, 0);
        player.eulerAngles -= new Vector3(0, 360 + Time.deltaTime * keysOrbitingSpeed, 0);

    }
    else if (Input.GetKey(KeyCode.D))
    {
        Debug.Log("Rotate Right");
        CenterPoint.eulerAngles += new Vector3(0, 360 + Time.deltaTime * keysOrbitingSpeed, 0);
        player.eulerAngles += new Vector3(0, 360 + Time.deltaTime * keysOrbitingSpeed, 0);
    }
}

Now the next part I am trying to achieve , when the camera is rotated / orbited around the player using the mouse and is left in a position that is not the origin i.e behind the player (in relation to player)… when A and D keys are pressed, what happens in GW2 is the camera seems to snap back to the origin and then continue to follow the player from the back ( or its relation origin with regards to the player Object if that makes sense)

This is what the game does (which I hope to achieve)
https://www.youtube.com/watch?v=qTTDaeuHl80

so at present moment with my current code
https://www.youtube.com/watch?v=a53lZ_0BDtg
camera and player will continue to rotate (with A and D), from where the camera was last left during orbit. How can I add to this, to force the camera to snap back to origin like they have in Guild Wars 2?

\$\endgroup\$
0
\$\begingroup\$
void Update () {
    Camera.main.transform.position = player.transform.position -  Quaternion.Euler(player.transform.rotation.eulerAngles) * transform.forward * distance;
    Camera.main.transform.rotation = Quaternion.Euler (0, player.transform.rotation.eulerAngles.y, 0);
}

It will change the position and rotation and camera to make it stay behind the player. If you need to move the camera smoothly you can use Vector3.Lerp for position and Quaternion.Lerp for rotation.

void Update () {
    desiredPos = player.transform.position - Quaternion.Euler (player.transform.rotation.eulerAngles) * transform.forward * distance;
    desiredRot = Quaternion.Euler (0, player.transform.rotation.eulerAngles.y, 0);
    Camera.main.transform.position = Vector3.Lerp (Camera.main.transform.position, desiredPos, Time.deltaTime*camPosSpeed);
    Camera.main.transform.rotation = Quaternion.Lerp (Camera.main.transform.rotation, desiredRot, Time.deltaTime*camRotSpeed);
}
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.