Search Unity

How can access to noise module in runtime?

Discussion in 'Cinemachine' started by Onsterion, Feb 13, 2018.

  1. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    211
    Hi,


    I am triyng to add a temporal shake effect in to the "Free Look" camera like normal games when you receive damage the camera temporally shakes but i can't find a access in the code for do that.
    Noise module is exposed?




    Regards.-
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,477
    There are a couple of ways to do this:
    1. With the noise module. Set a vibrating profile, and pulse it. See this thread: https://forum.unity.com/threads/how-to-shake-camera-with-cinemachine.485724/#post-3287851. The downside of this method is that you lose the ability to have handheld camera noise at the same time.
    2. Since that thread was posted, the CinemachineExtension API became available. That would be the preferred approach. Here is a simplistic example, to shake the camera continuously. Modify it to make it do a pulse instead:
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. /// <summary>
    5. /// An add-on module for Cinemachine to shake the camera
    6. /// </summary>
    7. [ExecuteInEditMode] [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    8. public class CameraShake : CinemachineExtension
    9. {
    10.     [Tooltip("Amplitude of the shake")]
    11.     public float m_Range = 0.5f;
    12.  
    13.     protected override void PostPipelineStageCallback(
    14.         CinemachineVirtualCameraBase vcam,
    15.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    16.     {
    17.         if (stage == CinemachineCore.Stage.Body)
    18.         {
    19.             Vector3 shakeAmount = GetOffset();
    20.             state.PositionCorrection += shakeAmount;
    21.         }
    22.     }
    23.  
    24.     Vector3 GetOffset()
    25.     {
    26.         return new Vector3(
    27.             Random.Range(-m_Range, m_Range),
    28.             Random.Range(-m_Range, m_Range),
    29.             Random.Range(-m_Range, m_Range));
    30.     }
    31. }
    32.  
     
    BrandStone and Onsterion like this.
  3. Onsterion

    Onsterion

    Joined:
    Feb 21, 2014
    Posts:
    211
    Perfect! Thanks.
     
unityunity