Search Unity

Custom extension with axis recenterin

Discussion in 'Cinemachine' started by Gandarufu, Nov 15, 2019.

  1. Gandarufu

    Gandarufu

    Joined:
    Nov 10, 2014
    Posts:
    22
    I'm trying to write a "Look Around" extension for a Vcam with a FrameTransposer. I managed to set up two axis for the mouse / gamepad input that I will use to offset (and rotate) the Camera.

    Code (CSharp):
    1. public class CinemachineCameraLookaround : CinemachineExtension
    2. {
    3.     private AxisState m_YAxis = new AxisState(-15, 15, false, false, 100f, 0.1f, 0.1f, "Mouse Y", false);
    4.     private AxisState m_XAxis = new AxisState(-15, 15, false, false, 100f, 0.1f, 0.1f, "Mouse X", false);
    5.  
    6.     private void Update()
    7.     {
    8.         m_XAxis.Update(Time.deltaTime);
    9.         m_YAxis.Update(Time.deltaTime);
    10.     }
    11.  
    12.     protected override void PostPipelineStageCallback(
    13.         CinemachineVirtualCameraBase vcam,
    14.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    15.     {
    16.         if (stage == CinemachineCore.Stage.Body)
    17.         {
    18.             state.PositionCorrection += new Vector3(m_XAxis.Value, m_YAxis.Value, 0);
    19.         }
    20.     }
    21. }
    I have a couple of questions.
    1) Is it okay to Update the Axis in the Update function, or is there a better way? (Cinemachine internally)
    2) How can I access / use the recenter function of the Axis?

    I'm struggeling to set up the recenter part. When I look in the inspector, the Axis already have the recentering part, but I don't know how to access it.

    Unbenannt.JPG


    I would appreciate any hint in the right direction :) Thanks in advance.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    1) yes, that's fine
    2) m_XAxis.m_Recentering.blabla

    I'm not too sure what behaviour you're looking for. With this code, you'll displace the vcam up/down/left/right a bit, but you won't rotate it. If you want to rotate it, then you can just write a script to modify the vcam's rotation in its transform - it doesn't need to be a CM extension. Or you can just put POV in the Aim.
     
  3. Gandarufu

    Gandarufu

    Joined:
    Nov 10, 2014
    Posts:
    22
    Thanks for your valuable and fast input, Gregoryl.

    My camera follows the player at a fixed camera angle. Upon mouse input I want to achieve a look around effect, as if the player moves his head, and thus his vision. Ultimately I want to achieve to rotate the camera around the player's head a bit, while still looking at player, so that you see more of the environment.

    Since transform.LookAt didn't work, I figured I'd split this effect into first moving the camera and then rotating it enough degrees to look back at the player (trigonometry ftw).


    Code (CSharp):
    1.      
    2. private void Update()
    3. {
    4.         xAxis_silent = !m_XAxis.Update(Time.deltaTime);
    5.         yAxis_silent = !m_YAxis.Update(Time.deltaTime);
    6.  
    7.         if (xAxis_silent)
    8.         {
    9.             m_XAxis.m_Recentering.DoRecentering(ref m_XAxis, Time.deltaTime, 0);
    10.         }
    11.         else
    12.         {
    13.            m_XAxis.m_Recentering.CancelRecentering();
    14.         }
    15.         if (yAxis_silent)
    16.         {
    17.             m_YAxis.m_Recentering.DoRecentering(ref m_YAxis, Time.deltaTime, 0);
    18.         }
    19.         else
    20.         {
    21.            m_YAxis.m_Recentering.CancelRecentering();
    22.         }
    23. }
    Do you know why in this example m_WaitTime has no effect? If I set up another
    private AxisState.Recentering m_XRecentering = new AxisState.Recentering(true, 1, 2);
    and switch out "m_XAxis.m_Recentering" with the new "m_XRecentering", the m_WaitTime is in effect...
     
    Last edited: Nov 15, 2019
  4. Gandarufu

    Gandarufu

    Joined:
    Nov 10, 2014
    Posts:
    22
    I'm essentially going for the effect you get with a Frame Transposer on the body and a POV on the air. However, I have damping and a Dead Zone set up on my Frame Transposer which results in a laggy, jagged movement. I tried to come up with a smooth solution for that.
     
    Last edited: Nov 15, 2019
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    You won't get the camera to rotate by manipulating state.PositionCorrection. You can try manipulating state.OrientationCorrection instead. That should give you what you want.

    What version of CM are you using? It could be that the recentering is buggy and ignores the wait time. If you get the very latest (still in preview) that should be corrected.
     
  6. Gandarufu

    Gandarufu

    Joined:
    Nov 10, 2014
    Posts:
    22
    Thanks. Do you happen to know if state.OrientationCorrection is in local space? I tried the following and I can't figure out how to rotate the camera (which is at a 40 degree angle facing down) around the world up axis.

    Code (CSharp):
    1. state.OrientationCorrection = Quaternion.AngleAxis(-calculatedAngle, Vector3.up);

    Using version 2.3.4 by the way...
     
    Last edited: Nov 15, 2019
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    It's in camera-local space