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.

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:
    6,078
    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:
    6,078
    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

    Unity Technologies

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

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,078
    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:
    5
    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:
    6,078
    Can you show me a picture of the vcam inspector?
     
    Last edited: Apr 25, 2019
  10. holahyunsik

    holahyunsik

    Joined:
    Apr 20, 2019
    Posts:
    5
    Same here. Can't access CinemachineComposer. Even after I add it by AddCinemachineComponent<CinemachinerComposer> cannot access to it. If I don't add it, it's always null but then when I actually add it and change ScreenY value, nothing happens.

    Code (CSharp):
    1. CinemachineComposer cc = vCam.GetCinemachineComponent<CinemachineComposer>();
    2.         cc.m_ScreenY = 0.25f;
     
    Last edited: Aug 18, 2020
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,078
    Yes, the help links are broken. Just google CinemachineVirtualCamera and you will find the docs.
     
  12. holahyunsik

    holahyunsik

    Joined:
    Apr 20, 2019
    Posts:
    5
    @Gregoryl Can you help me with this please?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

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

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    151
    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
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,078
    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.
     
  16. deadlycrow

    deadlycrow

    Joined:
    Feb 10, 2014
    Posts:
    151
    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
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,078
    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.
     
  18. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,287
    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?
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,078
    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?
     
  20. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,287
    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?
     
  21. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,078
    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.
     
  22. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,287
    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.
     
  23. 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.
     
  24. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    6,078
    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?
     
unityunity