Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing Cinemachine Cirtual Camera Noise values with C# script

Discussion in 'Cinemachine' started by Nadan, Aug 24, 2017.

  1. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341
    Hi,

    In my game the Cinemachine Virtual Camera has Noise (Handheld_wideangle_strong) and in the inspector I can see Amplitude Gain and Frequencey Gain values I can adjust.

    Is there possibility to access these values from another C# script? What I'm trying to do is when the player moves away from certain point the camera starts to shake more and more as player gets "sick and dizzy" in the game. So using the Vector3.distance I could add Amplitude Gain and Frequency gain higher when the player moves far enough.

    Any help?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Yes, you can do this from script.
    Code (CSharp):
    1. CinemachineVirtualCamera vcam;
    2. vcam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>().m_AmplitudeGain = bla;
     
  3. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341
    Hi,

    How can I change the Follow and LookAt transform with C#? In the beginning of the game I need to find player who is spawned for the virtual camera with GameObject.Find.

    I quess I need to access the m_Follow and m_LookAt in the Cinemachine Virtual Camera script.

    BTW. when I click the help button in the Virtual Camera component it takes me to this "Error 404 - Page not found" page: https://docs.unity3d.com/Manual/script-CinemachineVirtualCamera.html
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Yes.

    The Cinemachine documentation is not yet part of the Unity manual online. Until is it, we ship documentation directly with Cinemachine. You can access it from Cinemachine/About:

    upload_2017-12-1_8-43-22.png
     
    Nadan likes this.
  5. grossimatte

    grossimatte

    Joined:
    Mar 15, 2013
    Posts:
    43
    Still the documentation does not say how to access cinemachine parameters via c#.

    And your example:

    GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>()

    doesn't work.

    Do i have to import any specific package to access cinemachine parameters via c#?

    Thanks
     
  6. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
    earhod likes this.
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    You don't need to import anything. It should work. What parameters are you trying to access? Can you post the code that's giving you trouble? Also if you look at the CinemachineExamples, there is a scripting example there which demonstrates how to access the parameters.
     
  8. blindsp0t

    blindsp0t

    Joined:
    May 30, 2016
    Posts:
    6
    Hi,
    I'm having trouble to access the Path Offset property of a Virtual Camera, using c# script.
    Can you help ?

    I get a compiler error when trying this :
    Code (CSharp):
    1.            
    2. var off = vCam.GetCinemachineComponent<CinemachineCameraOffset>();
    3. off.m_Offset.x = xOffset;
    4.  
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Can you show me a picture of the vcam inspector?
     
    Last edited: Apr 25, 2019
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Yes, the help links are broken. Just google CinemachineVirtualCamera and you will find the docs.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    @holahyunsik That code snippet looks fine to me. It should work. The problem must lie somewhere in the context.
     
  12. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    I want to access this to control footsteps on a FPS camera setup i've done, thing is i have multiple virtual cameras, so is there a way to directly access some global noise parameter throught the cinemachine brain or something? my setup is simple, one idle camera (with its noise profile), walk cam, run, cam, etc. so, unless there can be some sort of event that can be triggered in the noise profile that i dont know off, i would like to trigger a footstep lets say, when some of the values of the noise profile is above 0.99f and below -0.99f(assuming a -1 to 1 fluctuation) and yes, i did a custom noise profile that resembles a head-bobbing when you walk/run, etc. any way to do this? or is it a better way lol? @Gregoryl
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    I'm not sure I fully understand your question. You are implementing head bobbing using perlin noise components on your vcam, with custom head-bobbing noise profile? And you want the bobbing to be turned on/off depending on some global setting? Perlin noise on the vcam probably isn't the best approach. Better would be either with Impulse (emit the bobbing impulses when your character walks) or with a custom extension that applies a perturbation in response to events that you control.
     
  14. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    166
    I was trying to do it the other way around, i already have the cameras set with the head-bobbing noise, what i asked, in short, is this: Can i access the current noise being played in the current camera by code? i figured that way i could sync my footstep sounds with the current head-bobbing, thats already there. Or is it possible in any way to trigger an event in an specific moment, previously setted in the noise profile asset? like pretty much what you do when you mess around with the curves and the events in the animation import settings of some characters animation? if thats not possible, it could be a nice feature for the future i think :p
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    I understand better now, thanks for the description.

    There is no event that can be fired from the noise profile. You could possibly write some code that monitors CinemachineBrain.State.CurrentCameraState.OrientationCorrection (or PositionCorrection, depending on how you set up your noise profile). The noise value will be in there. However, if you have other CM extensions (e.g. Collider, Confiner, others too) then they may also contribute to this value and confuse your code, so it's not foolproof.

    Much better would be to implement the footsteps with Impulse: emit one impulse per footstep. You can have multiple listeners that respond to the impulses, so it's a much better paradigm for what you're doing, I think.
     
  16. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,586
    I was looking for this, thank you.

    What I think is really useful in the ParticleSystem modules is that they provide additional "multiplier" properties (see here). This allows to drive particle values without having to backup the original value first.

    Rather than manipulating
    m_AmplitudeGain
    directly, Cinemachine could provide a
    m_AmplitudeGainMultiplier
    that is only used by user-code and initialized with 1.

    Internally, Cinemachine would calculate the amplitude gain as
    m_AmplitudeGain*m_AmplitudeGainMultiplier
    then.

    User code then only needs to modify
    m_AmplitudeGainMultiplier
    . Want to have 50%? Just specify 0.5.

    This allows to change Cinemachine properties easier than it's right now. It wouldn't even break backwards compatibility. What do you think?
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Well, that's kind of what amplitude gain already is: a multiplier for the noise coming in from the signal.
    Are you suggesting that we add this sort of thing for all vcam properties that someone might want to change by script?
     
  18. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,586
    I'm trying to say that properties, that don't mess with the original values, for things that are expected to be changed via scripting make working with the system nicer.

    For example, if I enter "2.66" as amplitude gain in the Inspector, I still want to use this "designed" value in my game when I modify the noise. In my particular case, a racing game, I want that the noise intensity depends on the speed.

    0mph = 0 amplitude gain
    60mph = the designed amplitude gain, in this case 2.66

    In game code I would calculate a weighting factor between 0..1 which represents the 0..60mph range, lets just call it
    t
    .

    Now I have several options...

    I can cache the designed amplitude gain:
    Code (CSharp):
    1. float m_DesignedAmplitudeGain;
    2.  
    3. void Start()
    4. {
    5.     m_DesignedAmplitudeGain = noise.m_AmplitudeGain;
    6. }
    7.  
    8. void Update()
    9. {
    10.     var t = CalcWeightingFactor(speed);
    11.     noise.m_AmplitudeGain = t * m_DesignedAmplitudeGain;
    12. }
    I can ignore the designed value in the Inspector and hard-code it:
    Code (CSharp):
    1. void Update()
    2. {
    3.     var t = CalcWeightingFactor(speed);
    4.     noise.m_AmplitudeGain = t * 2.66;
    5. }
    6.  
    Or what I would prefer and think it's the better solution of the three, I set a multiplier and not the actual value as shown below.
    Code (CSharp):
    1. void Update()
    2. {
    3.     var t = CalcWeightingFactor(speed);
    4.     noise().m_AmplitudeGainMultiplier = t;
    5. }
    6.  
    where
    noise
    represents
    vcam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>()
    .

    Is this description better?
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    I already understood the ask, and I get it. It makes sense. It's just hard to know where to draw the line for things that are "expected" to change. People make all kinds of stuff, and I am often amazed at what people try to do. It's quite wonderful, actually. The thing is, we have to weigh the advantage of the convenience of the extra setting against the disadvantage of adding more clutter and complexity to an already complex and cluttered system.
     
  20. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,586
    Yes, I agree. I would probably approach this on a per-request basis, rather than adding it everywhere from the get-go. If you have further insight into what values users most often modify, then these could be good candidates for multiplier properties as well.
     
  21. GDolla

    GDolla

    Joined:
    Mar 11, 2021
    Posts:
    1
    Hello, we have character for streaming on twitch and we need to set up camera to make it look on character and have just a little moves like camera noise (like somebody holding it). We have 2018.4.31f1 version. We tryed cinemachinebut it doesnt work with it.
     
  22. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Can you be more specific about what you tried? Cinemachine has a camera noise feature that works perfectly well. What version of Cinemachine are you using?
     
  23. LouWorldOrder

    LouWorldOrder

    Joined:
    Oct 24, 2022
    Posts:
    14
    Alright, I've got an issue. Let me know if I need to make a separate topic for this, but it seems quite related to the discussion at hand.

    I am trying to add noise, upon a collision. I am using Cinemachine but... I am using a State Driven Camera and this seems to complicate matters quite a lot. I have been able to find examples for if I was using just the regular camera, or if I was using a single Freelook Cinemachine camera, but the only example that used state driven... The code was 3-4 years old and I couldn't get it to work.


    He shows the code that was giving me an error (on get) around 5:42.

    So in my script where I detect the collision with the player, I have:
    Code (CSharp):
    1. using Cinemachine;
    After declaring the public class:
    Code (CSharp):
    1. CinemachineStateDrivenCamera cinemachineStateDrivenCamera;
    But in Start it is giving me an error:
    I am trying to create the noise component (to trigger its increase upon the collision) using:
    Code (CSharp):
    1. CinemachineBasicMultiChannelPerlin noise = cinemachineStateDrivenCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
    2.  
    Both that and regular GetComponent is giving me errors in the compiler. Does not contain a definition and no accesible extension method.

    Then where I want it triggered:
    Code (CSharp):
    1. public void OnTriggerEnter2D(Collider2D other)
    2. {
    3. if(other.gameObject.CompareTag(
    4. Player))
    5. {
    6. noise.m_AmplitudeGain = 5.0f;
    7. }
    Any advice on where I am going wrong would be greatly appreciated!
     
  24. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    The example video is very old.

    A better way to get shakes on collision is to use Cinemachine Impulse. There are a number of tutorials that you can find with google, here is one:


    If you want to shake based on entering a trigger zone or colliding with an object, add a CinemachineCollisionImpulseSource component to you Collider2D, and set it up to emit an impulse signal when objects collide with it. Then add a CinemachineImpulseListener extension to the vcam, or a CinemachineIndependentImpulseListener behaviour to the main camera, which is effectively a global camera shaker that will affect all vcams.
     
    LouWorldOrder likes this.
  25. LouWorldOrder

    LouWorldOrder

    Joined:
    Oct 24, 2022
    Posts:
    14
    I'll give that a look. Thanks so much.
     
  26. LouWorldOrder

    LouWorldOrder

    Joined:
    Oct 24, 2022
    Posts:
    14
    It worked, thanks again. I am a little worried about the consistency of the shake. Will this be affected (in a big way) by the blending of two cameras, esp. if there is a lot of zooming happening in the blend? I guess I will have to play around with it, now that it is implemented.
     
    Gregoryl likes this.