Search Unity

Question How to Create Multiple Camera Shake Patterns

Discussion in 'Cinemachine' started by Apeles, Sep 3, 2022.

  1. Apeles

    Apeles

    Joined:
    May 16, 2022
    Posts:
    5
    Hello everyone,

    I would like to implement multiple Camera Shake Patterns using Cinemachine, which I would like to access from any script. What would be the best practice to do this?

    I have searched the forums and found out that using Impulse is preferred to controlling the camera Noise from the script. However, I am not sure how to add multiple impulses and trigger them by calling different functions.

    In my game, I made a script called "CameraController" (attached to empty GameObject) which I use for switching the active camera and everything related to cameras. It seems that It would be quite logical to place Camera shake methods here and then turn this script into Singleton, so I can access it from any other script. Is this approach correct?

    The part that confuses me the most is how to actually make multiple shake patterns and effectively control them all in one place. Should I create ImpulseSource for every single camera shake pattern?

    It would be very nice if somebody could give just a very basic example of how to do this.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Each ImpulseSource will emit a specific impulse. So, you can have multiple ImpulseSource children of your singleton object, or you cam make a custom script with an array of CinemachineImpulseDefinition objects, one for each impulse type. Have a look at CinemachineImpulseSource.cs, you'll see that it's quite simple and it should be fairly straightforward to implement a variant of it.
     
    Apeles likes this.
  3. Apeles

    Apeles

    Joined:
    May 16, 2022
    Posts:
    5
    @Gregoryl Thank you for answer!
    Yes, a custom script with an array of CinemachineImpulseDefinition objects, one for each impulse type sounds like a good idea. I would then set active CinemachineImpulseDefinition like this
    Code (CSharp):
    1. _impulseSource.m_ImpulseDefinition = impulseDefinition;
    Is that okay?

    What worries me is that I will then have to create a lot of very similar methods where in each method the corresponding CinemachineImpulseDefinition will be set as active. So that's a lot of repeated code. I don't know how to send CinemachineImpulseDefinition as a parameter of the method from another script (maybe the name of CinemachineImpulseDefinition or enum). Is there a better way to do this? What would be the best practice? Sorry If I am asking too much now...
    Thanks in advance!
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    I'm suggesting that you make your own variant of CinemachineImpulseSource (just copy the file, rename it, and modify). In this variant, instead of having a single CinemachineImpulseDefinition, it would have an array. And the GenerateImpulse method would take an index parameter and use that index to access the array and generate the appropriate signal. No repeated code.