Search Unity

Question CinemachineMixingCamera with POV cameras.

Discussion in 'Cinemachine' started by br0br0br0, Jan 20, 2023.

  1. br0br0br0

    br0br0br0

    Joined:
    Feb 26, 2014
    Posts:
    10
    I'm trying to create an orbital vehicle camera rig that blends between two cameras based on the vehicle's speed. I'm using a CinemachineMixingCamera with two pov cameras and using the speed to control the weights.

    However when changing the weights the pov values aren't being inherited like they would if I were blending between two virtual cameras with Inherit Position checked.

    As the vehicle's speed changes, this causes the camera to blend back to a different position if the pov values of one of the virtual cameras has been changed.

    Any suggestions? Is this the correct approach?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    When you set the weights within the MixingCamera, avoid setting them to 0. Instead, use a very small weight. That way, all POV children will remain live and continue to process mouse events.
     
    gaborkb likes this.
  3. br0br0br0

    br0br0br0

    Joined:
    Feb 26, 2014
    Posts:
    10
    Thanks, but no good unfortunately. I was using the cinemachine input provider but have removed and confirmed the same thing happens with mouse events. Both cameras are live but only 1 of the povs appears to recieve the input events.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    It works for me here. Which version of Unity are you using? What was the very small weight you used?
     
  5. br0br0br0

    br0br0br0

    Joined:
    Feb 26, 2014
    Posts:
    10
    Ah yeah you're right, I've got it working. I'm on 2022.2.0b10 and using 0.001f as a small weight value.

    I narrowed down the issue I was having by manually changing one of the weights in the MixingCamera inspector and noticed it blended correctly. However in my code I was changing both weights during the same LateUpdate:
    Code (CSharp):
    1. float lNormalisedSpeed = Mathf.Clamp01(lSpeed / m_FarCameraMinSpeed);
    2. mMixerCamera.SetWeight(0, Mathf.Max(1.0f - lNormalisedSpeed, 0.001f));
    3. mMixerCamera.SetWeight(1, lNormalisedSpeed);
    I've changed my code to only update one of the weights and that's fixed my issue:
    Code (CSharp):
    1. float lNormalisedSpeed = Mathf.Clamp01(lSpeed / m_FarCameraMinSpeed);
    2. mMixerCamera.SetWeight(0, Mathf.Lerp(0.001f, 2.0f, lNormalisedSpeed));
    Perhaps the pov values aren't being updated during the same frame the weights are set causing them to become out of sync?
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Setting both on the same frame should work.
    What is your CinemachineBrain update mode?
     
  7. br0br0br0

    br0br0br0

    Joined:
    Feb 26, 2014
    Posts:
    10
    The Brain Update Method is set to Smart Update and the Blend Update Method is set to Late update.

    I'm also setting the weights in LateUpdate, that correct?
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    This means that the vcams and mixingcamera is being updated from CinemachineBrain.LateUpdate. Unless the script containing your LateUpdate has an execution order later than CinemachineBrain, then your weight modification should take effect immediately. Otherwise, it would take effect on the next frame.

    EDIT: oh wait a minute. Can you do a quick test and change your CMBrain's UpdateMode to LateUpdate? What happens if you do that?

    EDIT2: Before trying that, try a larger "very small weight", say 0.01.
     
  9. br0br0br0

    br0br0br0

    Joined:
    Feb 26, 2014
    Posts:
    10
    I think I've figured out what was happening.

    You're absolutely right about being able to set both weights in the same frame and needing a very small weight (0.001 was fine in the end). However if one of the weights is set to 0 when the camera first becomes active it'll throw off the pov values (even if you then change the weight to something greater than 0).

    My issue was that during the first frame of the camera becoming active (but before my code to clamp the weight to a very small value), it's weight was unintentionally being set to 0.

    Hope that makes sense?

    Thanks for your help, let me know if you need any more info.
     
    Last edited: Jan 26, 2023
    Gregoryl likes this.