Search Unity

Controlling audio with cinemachine/Timeline

Discussion in 'Cinemachine' started by AndyNeoman, Jul 24, 2019.

  1. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Hi there, How do you all go about controlling audio fading in and out with cinemachine? Also volume levels. I woulds like to fade down the volume on some assets while the cutscene plays then turn them back up at the end and vice versa.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
  3. Onat-H

    Onat-H

    Joined:
    Mar 11, 2015
    Posts:
    195
    You can use this Cinemachine Extension we wrote, to blend from on Mixer Snapshat to another, perfectly timed with the camera transition. Hope it helps :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Audio;
    3. using Cinemachine;
    4.  
    5. [ExecuteInEditMode]
    6. [SaveDuringPlay]
    7. [AddComponentMenu("")] // Hide in menu
    8. public class CinemachineSnapshotTransition : CinemachineExtension
    9. {
    10.     [Tooltip("Transition to this Mixer Snapshot")]
    11.     public AudioMixer audioMixer;
    12.     public AudioMixerSnapshot snapshot;
    13.     public float preDelay     = 0.0f;
    14.     public float lengthOffset = 0.0f;
    15.  
    16.     private float time;
    17.     private CinemachineBrain brain;
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         brain = Camera.main.GetComponent<CinemachineBrain>();
    23.         brain.m_CameraActivatedEvent.RemoveListener(OnCameraActivated);
    24.         brain.m_CameraActivatedEvent.AddListener(OnCameraActivated);
    25.     }
    26.  
    27.  
    28.     protected override void PostPipelineStageCallback( CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime )
    29.     {
    30.        
    31.     }
    32.  
    33.  
    34.     private void Transition()
    35.     {
    36.         snapshot.TransitionTo(time);
    37.     }
    38.  
    39.  
    40.     void OnCameraActivated(ICinemachineCamera dst_camera, ICinemachineCamera src_camera)
    41.     {
    42.         if (dst_camera == null || src_camera == null)
    43.             return;
    44.  
    45.         if ( (CinemachineVirtualCameraBase)dst_camera == VirtualCamera )
    46.         {
    47.             CinemachineBlendDefinition blend = brain.m_CustomBlends.GetBlendForVirtualCameras(src_camera.Name, dst_camera.Name, brain.m_DefaultBlend);          
    48.             time = Mathf.Max(blend.m_Time + lengthOffset, 0.0f);
    49.  
    50.             Invoke("Transition", preDelay);
    51.         }
    52.     }
    53. }
    54.  
     
    Gregoryl likes this.