Search Unity

Question Virtual Camera POV and controls

Discussion in 'Cinemachine' started by Nashnir, Aug 14, 2020.

  1. Nashnir

    Nashnir

    Joined:
    Oct 30, 2015
    Posts:
    23
    Apologize for the long post but I couldn't make it shorter without sacrificing clarity.

    My Hierarchy is as follows:
    The Camera with the Brain, The Cinemachine Virtual Camera and the Player Gameobject
    Hierarchy Setup.PNG

    My settings on the Virtual Camera
    Virtual Camera Settings.PNG

    What I am trying to achieve.

    Objective 1: When there is no keyboard Input from user (WASD) but only mouse, the camera behaves like a free look camera.
    -----------------------------------------------------------------------------------------------------
    This is currently already achieved in due to the Aim Vertical and Horizontal Range as shown below.
    Camera Free Look.PNG

    Objective 2: When the the camera reaches the edges of its horizontal view range limits (-85 to 85), as the user moves the mouse, the body rotates as well.
    -----------------------------------------------------------------------------------------------------
    The code for the player rotation:
    Code (CSharp):
    1. player.transform.Rotate(new Vector3(0.0f, Mathf.Sign(player.playerCamera.currentUserInput.x) * player.playerCamera.horizontalSpeed * Time.fixedDeltaTime, 0.0f), Space.Self);
    After that Virtual Camera transform is aligned with the player transform. This is done since they are both on the same hierarchy level.
    Code (CSharp):
    1. player.playerCamera.playerFPVCamera.transform.rotation = Quaternion.RotateTowards(player.playerCamera.playerFPVCamera.transform.rotation, player.transform.rotation, 360f);
    The above method has a small problem.

    The problem is while the Virtual Camera transform updates, the camera horizontal aim range remains aligned with the old transform for some reason as shown below. Player Alignment.PNG

    This can be solved by a change in hierarchy levels as shown below and not using code to update camera transform rotation but I am not sure why the issue is cause in the first place.
    Hierarchy Setup 02 .PNG

    The reason I do not wish to hierarchy as shown in the image above is because of the following objective.

    Objective 3: When the player provides move input (WASD), the body aligns to the camera view and moves in that direction. If I hierarchy as in the image above, it causes a loop.
    -----------------------------------------------------------------------------------------------------

    I am looking for a solution to achieve all my objectives. Any help will do
     
    Last edited: Aug 14, 2020
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I would make the vcam a child of the player, positioned at the correct offset, and change the Body section to Do Nothing, since procedural following is no longer required.

    For objective 3: when there is a WASD keypress, align the player to the vcam Y rotation, and then set the vcam's Y rotation to 0 relative to the parent.