Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How To Change Blend Speed Between Two CineMachineVirtualCameras through Code

Discussion in 'Cinemachine' started by Payaso_Prince, Jul 24, 2023.

  1. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    Hello,

    Will someone please provide me with a simple example of dynamically changing the blend speed between two cameras through code?

    I looked at the CinemachineBlend Class documentation but can't seem to figure it out.

    Thanks so much for taking the time!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,207
    Sorry for the slow response. Can you be more specific about what you want to do?
    • Are you trying to alter the blend time while the blend is in progress?
    • or are you trying to programmatically set a blend time when a blend is initiated?
    • or are you looking to have a continuous blend whose weights you adjust dynamically?
     
    Payaso_Prince likes this.
  3. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    Thanks for the reply! :)
    I'll try and be more specific.

    I have two CinemachineVirtualCameras:
    cameraA
    cameraB

    The blend speed from cameraA to cameraB is 1 second.
    I'd like to be able to change the blend speed of cameraA to cameraB to different amounts based on what's happening in my game. So, sometimes I'd like the blend speed to be quicker or longer.

    How can I adjust the blend speed from cameraA to cameraB through code?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,207
    In that case, you can install a callback that gets invoked whenever a new blend is starting. In that callback, you can modify any aspect of the blend. See CineamchineCore.GetBlendOverride.
     
    Payaso_Prince likes this.
  5. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    85
    Thanks so much for offering a solution!

    I checked out the documentation, but I'm still pretty hazy on actually implementing it. I don't normally work with delegates, so I'm unfamiliar with the correct syntax.

    Would you be able to provide me with a simple example of using GetBlendOverride to change the blend speed?
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,207
    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. public class CustomBlendTimeManager : MonoBehaviour
    5. {
    6.     private void Awake()
    7.     {
    8.         CinemachineCore.GetBlendOverride = BlendOverrideHandler;
    9.     }
    10.  
    11.     CinemachineBlendDefinition BlendOverrideHandler(
    12.         ICinemachineCamera fromVcam, ICinemachineCamera toVcam,
    13.         CinemachineBlendDefinition defaultBlend,
    14.         MonoBehaviour owner)
    15.     {
    16.         // Make this decision depending on whatever internal criteria you have
    17.         if (needToCustomizeBlend)
    18.         {
    19.             var customBlend = defaultBlend;
    20.             customBlend.BlendTime = ...
    21.             return customBlend;
    22.         }
    23.         return defaultBlend;
    24.     }
    25. }
    26.  
    27.