Search Unity

Rotating Camera Back Around To Face Player's Back

Discussion in 'Scripting' started by Deleted User, Sep 20, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hiya guys,

    I am having some trouble with my camera at the moment. I want it to act pretty much like the camera's you find in MMORPG's, where if you release the right mouse button it swerves back around to face the player's back. Instead, all I can get is the camera to look at the player, i.e. through the transform.LookAt() method.

    I don't want to child the camera to the player or vice versa, as I have two characters the Player can select, like in-game character selection. Thinking of Final Fantasy here.

    Code (CSharp):
    1.     private void LateUpdate()
    2.     {
    3.         float yaw = Input.GetAxisRaw("HorizontalStick");
    4.  
    5.         if (yaw != 0)
    6.         {
    7.            currentPlayer.rotation = Quaternion.Euler(0, 25 * yaw, yaw);
    8.             transform.RotateAround(currentPlayer.position, Vector3.up, yaw);
    9.         }
    10.         else
    11.         {
    12.             offsetFromPlayer = new Vector3(transform.position.x, currentPlayer.position.y + offsetY, currentPlayer.position.y + offsetZ) - currentPlayer.position;
    13.             Vector3 pos = currentPlayer.position + offsetFromPlayer;
    14.             transform.position = Vector3.Slerp(transform.position, pos, 0.02f);
    15.             transform.LookAt(currentPlayer.position);
    16.         }
    17.     }
    This is my code or at least everything that is not commented. The offsetY, and offsetZ are purely floats that give the camera some distance to the player. The "HorizontalStick" is the gamepad stick (5 Axis) on an XBox gamepad, for moving left and right.