Search Unity

saving and setting cameras position and rotation

Discussion in 'Cinemachine' started by steveh2112, Jan 26, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i'm using cinemachine as a free look 3rd person camera and in some cases i want to focus on something else for a few seconds.

    i got it working like this
    Code (CSharp):
    1. public static void SetPlayer3rdPersonCameraLookAt(Transform t, float distance=0)
    2.     {
    3.         CinemachineFreeLook.Orbit[] florbits = _CinemachineFreeLook.m_Orbits;
    4.         if (t == null)
    5.         {
    6.             t = _Player.transform;          
    7.             florbits[0].m_Radius = florbits[1].m_Radius = florbits[2].m_Radius = 3; // default for player
    8.         }
    9.         else
    10.         {
    11.             if(distance != 0)
    12.             {
    13.                 // this should happen only at the first time it called for alternate camera
    14.                 florbits[0].m_Radius = florbits[1].m_Radius = florbits[2].m_Radius = distance;  // set following distace
    15.  
    16.                 Debug.Log("x=" + _CinemachineFreeLook.m_XAxis.Value);
    17.             }
    18.                
    19.         }
    20.         _CinemachineFreeLook.LookAt = t;
    21.         _CinemachineFreeLook.Follow = t;
    22.     }
    but when this is called with null to return to the players camera, its facing the wrong way.
    how would i save the way its facing so i can return to the player looking at her bum?
    thanks
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    You're doing it the hard way. The easy way is to instance a new vcam that's positioned and oriented the way you want it, then enable it and let the Brain make the smooth transition. When it's time to revert, simply disable it and the Brain will transition back.

    For the best transition quality, set the new vcam's LookAt target to point to the gameObject you're looking at.
     
    steveh2112 likes this.
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    thanks, just found this
    https://forum.unity.com/threads/programatically-switch-between-virtual-cameras.483165/

    in the project i have one vcam for the player already, so i added another for object following which is disabled by default, then just enable and disable the cams in script as needed. seems to work fine, thanks

    one last question, when i enable the object following camera, it starts out life in a collision with the terrain and then has to adjust. is there a simple way to force it to find an unobscured view of the target before it enables?

    thanks again
     
    Last edited: Jan 28, 2020
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    That depends on your vcam's setup.
    How does it adjust to get out of collision with the terrain?
    Can you post an image of the vcam's inspector?
     
  5. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    its working great but the issue is, my character is pulling something towards itself. at a certain point the object falls through the floor. at that point i switch the camera to follow the falling object, but the camera starts out at the characters current 3rd person camera view position.

    the object quickly falls out of sight of the camera as it falls so the camera adjusts itself to find a better view, which is all working the way its supposed to.

    except it doesn't look great because the camera is searching for a better view as the object is falling through the floor.

    i would prefer to set a prefered point in world coords for the object following camera to start out at. i guess i can simply set its position come to think of it, i'll try it.

    thanks
     
  6. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    ok, i got it working perfect now, here is my camera switcher code
    Code (CSharp):
    1. public void SetObjectFollowCameraLookAt(Transform t, Vector3 camStartPosition, float distance = 0)
    2.     {
    3.         CinemachineFreeLook flc = _ObjectFollowCamera.GetComponent<CinemachineFreeLook>();
    4.         CinemachineFreeLook.Orbit[] florbits = flc.m_Orbits;
    5.         if (distance != 0)
    6.         {
    7.             // is distance was 0, keep distance set in CameraFalling
    8.             florbits[0].m_Radius = florbits[1].m_Radius = florbits[2].m_Radius = distance;  // set following distace
    9.             if(camStartPosition != Vector3.zero)
    10.                 _ObjectFollowCamera.transform.position = camStartPosition;
    11.         }
    12.  
    13.         flc.LookAt = t;
    14.         flc.Follow = t;
    15.        
    16.         _Player3rdPersonCamera.SetActive(false);
    17.         _ObjectFollowCamera.SetActive(true);
    18.     }
    the caller sets an ideal spot to move the camera to initially, so i set that above the player and in an area clear of obstacles. the cam transitions to that spot as it follows the falling object, looks very professional now. thanks again for all your help
     
    Gregoryl likes this.
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Why are you using a FreeLook for the object follow camera? Just use an ordinary simple CinemachineVirtualCamera with a Transposer and Composer.
     
  8. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    because i don't know how to do that. i've only ever used the freelock.

    i actually kind of like that the mouse guides the camera in case the user wants a different view of the target object. i should make the hold time before it returns to the user a configurable parameter.

    i'm making a game builder game so i want to provide scripts to my users.