Search Unity

smooth transistion to new offset

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

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i have a third person aim camera which works fine using this
    Code (CSharp):
    1. public class CameraAim : CinemachineExtension
    2. {
    3.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    4.     {
    5.         if (stage == CinemachineCore.Stage.Aim)
    6.         {
    7.             //Debug.Log("PostPipelineStageCallback Aim");
    8.             state.PositionCorrection += state.FinalOrientation * Offset;
    9.         }
    10.     }
    11. }
    but i'd like a smooth transition to the aim position so i wrote this
    Code (CSharp):
    1. public class CameraAim : CinemachineExtension
    2. {
    3.     public Vector3 Offset;
    4.     public Vector3 OffsetInc;
    5.     public Vector3 CurrOffset;
    6.     protected override void Awake()
    7.     {
    8.         OffsetInc = Offset / 100;
    9.         CurrOffset = new Vector3(0, 0, 0);
    10.     }
    11.  
    12.  
    13.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    14.     {
    15.         if (stage == CinemachineCore.Stage.Aim)
    16.         {
    17.             //Debug.Log("PostPipelineStageCallback Aim "+ deltaTime);
    18.             if (CurrOffset.z < Offset.z) {
    19.                CurrOffset += OffsetInc;
    20.             }
    21.             state.PositionCorrection += state.FinalOrientation * OffsetInc;
    22.         }
    23.     }
    24. }
    maybe its the wrong way to do it, please advise?
    anyhow, Awake is only called once and i can't find an enter callback so no idea where to put the initialization code so it only works the first time it's used.

    any idea? thanks
     
  2. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i figured it out if anyone interested, my player aiming camera offset script looks like this
    Code (CSharp):
    1. public class CameraAim : CinemachineExtension
    2. {
    3.     public Vector3 Offset;
    4.     public float OffsetMultiplier;
    5.  
    6.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    7.     {
    8.         if (stage == CinemachineCore.Stage.Aim)
    9.         {
    10.             state.PositionCorrection += state.FinalOrientation * Offset * OffsetMultiplier;
    11.         }
    12.     }
    13. }
    the extra control OffsetMultiplier is set in the animation state machine callback like this
    Code (CSharp):
    1.     override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    2.     {
    3.         // Implement code that processes and affects root motion
    4.         AnimatorStateInfo animationState = animator.GetNextAnimatorStateInfo(0);
    5.         if (animationState.normalizedTime != 0)
    6.             return;     // its in a transition from another state i think, confused much!
    7.  
    8.         animationState = animator.GetCurrentAnimatorStateInfo(0);
    9.  
    10.         // smooth trasition camera to aim offset
    11.         _PlayerController.Player3rdPersonCamera.GetComponent<CameraAim>().OffsetMultiplier = animationState.normalizedTime;
    12.     }