Search Unity

Reset Follow Camera to be behind player

Discussion in 'Cinemachine' started by DrewofDonuts, Oct 4, 2021.

  1. DrewofDonuts

    DrewofDonuts

    Joined:
    Apr 23, 2020
    Posts:
    28
    Hi!

    Relevant script elements:
    * Third Person Vcam "AimCam"
    * FollowCam followCam"
    * GameObject " _followTarget", same use as in the tutorial, where the AimCam follows it as the player controls its rotation

    I used the unity Tutorial to implement a 3rd person controller, but I would like to the player to smoothly rotate to the follow target's forward rotation as opposed to snap.


    I was able to make this work by implementing a FreeLook camera, and using the following code (only when the player is moving forward or back):

    Code (CSharp):
    1.         var projectedCameraForward = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up);
    2.         var targetAngle = Quaternion.LookRotation(projectedCameraForward, Vector3.up);
    3.  
    4.         _followOffensiveTarget.transform.rotation =
    5.             Quaternion.RotateTowards(_followOffensiveTarget.transform.rotation, targetAngle,
    6.                 playerTurn * Time.deltaTime);

    The problem that this introduced was that the FollowCam will remain in its last position after I switch to the AimCam, and so when I switch back to the FollowCam, it's facing the "wrong" direction and leads to wonky transitions. For the life of me cannot get it to just reset behind the player.

    I believe the solution is something like
    freeLook.m_XAxis.Value = [insert player's vector somehow from a project plane], but I cannot figure out how to actually code it.

    Any ideas would be great - I've burned so many hours trying to figure this out.

    Thank you in advance!


     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Have you tried checking this box?

    upload_2021-10-5_12-2-48.png