Search Unity

Free look - Reset pos behind player with simple world up

Discussion in 'Cinemachine' started by mrCharli3, Dec 30, 2018.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    418
    Is there any way to set the camera position behind the player via script? i.e reset it. Currently when my player respawns it's a bit annoying to rotate the camera every time.
    It has to be "Simple world up"

    Thanks!
     
    Last edited: Dec 30, 2018
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    Deactivate the vcam game object, place it where you want it, then reactivate it.
     
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    418
    Okay. The thing is I'm not sure which values to modify.
    I would assume its the X axis value, but that one always stays at 0 even when Im moving the camera. Only the Y axis value changes.
    And I can't set it using transform (world) position because that changes all the time.

    I should add that the camera both follows and looks at the player at all times.
     
    Last edited: Dec 30, 2018
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    I thought that you want to position it when the player respawns. Is that true?
    In SimpleFollow, the X axis stays at 0. Put the Y axis at 0.5 to place it on the middle orbit.
    Position the vcam's transform to be on the middle orbit, behind the player, i.e. playerPos - (player.forward * orbitRadius) + (orbitHeight * Vector3.up)
     
  5. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    418
    Thanks man,

    I did my best to translate that into code, and setting the Yaxis value works fine, but cannot seem to effect the actual transform:

    Code (CSharp):
    1.  
    2.             public CinemachineFreeLook vcam;
    3.             vcam.m_YAxis.Value = 0.5f;
    4.  
    5.             Vector3 playerPos = ActivePlayer.gameObject.transform.position;
    6.             Vector3 playerForward = ActivePlayer.gameObject.transform.forward;
    7.             vcam.VirtualCameraGameObject.transform.position = playerPos - (playerForward * vcam.m_Orbits[1].m_Radius) + (vcam.m_Orbits[1].m_Height * Vector3.up);
    Tried a few different ways but can't seem to effect the vCams transform at all.
    Maybe I'm misunderstanding you because I can't move the transform of the camera through the editor either.
     
    Last edited: Jan 1, 2019
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    You can only alter the vcam's transform while the vcam is disabled, otherwise it will overwrite whatever you do. Disable it, set its transform, then re-enable it.
     
    hushuyan002682 and mrCharli3 like this.
  7. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    40
    Don't works. Only changing axes values changing freelook position, but not resetting it to initial. For example, here is initial camera position:
    Screenshot_20190830_104507.png
    Trying to reset position with initial axes values:
    Code (CSharp):
    1. _freeLook.PreviousStateIsValid = false;
    2. _freeLook.m_XAxis.Value = 1;
    3. _freeLook.m_YAxis.Value = 0;
    Result:
    Screenshot_20190830_105727.png
    It's possible to reset freelook camera position to initial?
     
  8. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    40
    PS: freelook settings Screenshot_20190830_110428.png
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    Your binding mode is SimpleFollow. That binding mode is special, because it's relative to camera forward, and has no knowledge of the target's orientation. If you want to position the vcam as a function of character orientation (e.g. behind the character by default) then you should use LockToTargetWithWorldUp binding mode.
     
  10. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    40
    I want to (smoothly) move camera behind character, when player press particular key, but allow rotation camera around otherwise. Maybe there are some predefined setting for such behavior? Changing axis values or Vcam transform ( https://forum.unity.com/threads/fre...yer-with-simple-world-up.605176/#post-4052194 ) with enabling and disabling of game object seems to be not working even with LockToTargetWithWorldUp mode
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    If your binding mode is LockToTarget, then you need to lerp the axis value to 0. You can't set the transform, as that will be immediately overwritten because it's procedurally calculated by the vcam as a fucntion of the X axis value and the target's position and rotation.

    If your binding mode is SimpleFollow, then you may disable the vcam, set its transform behind the player as described above, and then re-enable it. If you want that movement to be gradual instead of instantaneous, then you'll have to do that gradually over many frames.

    Alternatively, instead of setting the transform, you could (every frame) dynamically calculate what axis value would put the camera behind the player, then SmoothDamp your way there. Note that the required axis value will be different every frame, as it represents the angle between current camera forward and target camera forward. When you get there the value will be 0.
     
    syjgin likes this.
  12. syjgin

    syjgin

    Joined:
    Feb 13, 2015
    Posts:
    40
    Thank you, previous method is works now, but I used wrong axis values. Now all works as expected
     
    Gregoryl likes this.