Search Unity

How to set PovCustom's view direction to Composer's

Discussion in 'Cinemachine' started by CabbageOni, Feb 10, 2018.

  1. CabbageOni

    CabbageOni

    Joined:
    Nov 22, 2016
    Posts:
    3
    Hello, currently I'm creating a cinematic scene in FPS using Cinemachine.

    My timeline has this routine:

    POVCustom(FPS control with mouse) -> Composer(Cinematic happens) -> POVCustom(back to FPS control again)

    however when returning from composer to POVcustom, I want the POVcustom to save the view direction of Composer's so instead of going back to where player was watching, player can continue the game from where composer was watching.

    What I first tried was:
    Code (CSharp):
    1. Vector3 lookAtVector = composer.LookAtTarget.position - composer.transform.position;
    2. lookAtVector.Normalize();
    3.  
    4. //vector to euler (dirty code, do not do this)
    5. povCustom.m_HorizontalAxis.Value = lookAtVector.x == 0 ? 0 : Mathf.Rad2Deg * Mathf.Atan(lookAtVector.z / lookAtVector.x);
    6. povCustom.m_VerticalAxis.Value = -Mathf.Rad2Deg * Mathf.Acos(lookAtVector.y);
    which is very messy and it also doesn't work.
    Is there any better solution rather than manually putting composer values into Horizontal/VerticalAxis?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,712
    Interesting use-case. In general your approach is correct: get the camera's look-at vector and try to match up the X and Y axis values of the FreeLook to it.

    For the composer's lookAt, you can use Vector3 dir = vcam.State.RawOrienation * Vector3.forward.
    Matching the FreeLook's axes to it is a little more tricky.

    Let's first deal with the X axis, that's easier because the X axis value units are just the Y rotation in degrees. You didn't mention the FreeLook's binding Mode. That will make a difference. I'll assume World binding, and heading bias == 0; In that case, the X-axis value should be set to the signed angle (in degrees) that the dir's projection onto the XZ plane makes with Vector3.forward.

    The Y axis is another story. The units range from 0 to 1, where 0 is the bottom rig's position, 1 is the top rig's, and 0.5 is the middle rig's position. Because you can place the rigs' heights and radii as you please, and also play with the spline tension, the value doesn't have a fixed relationship to any angle, which is why your formula wasn't working. You'll have to work out the math for yourself, depending on how you set up the orbits. Perhaps easier, you could try a little initializer routine that just samples the spline at various points, computes the angle, and sets up a look-up table to make the correspondence.
     
    Last edited: Feb 11, 2018