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.
  2. Dismiss Notice

Camera shake oriented relative to character's current forward direction

Discussion in 'Cinemachine' started by v2-Ton-Studios, Oct 31, 2021.

  1. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    237
    (Is it no longer possible to search through the forums? Can't seem to find a way to do it. So sorry if this is a dup.)

    https://docs.unity3d.com/Packages/c...ineImpulseFixedSignals.html#CreateFixedSignal

    Top down game, I'd like a camera shake to occur along an orientation, ex. according to the direction my character is currently facing, the direction from which something hits my character, etc.

    My current guess is to use something as follows...

    - Add an Impulse Source to my character and a listener to my camera.
    - Create a new Fixed Signal, which operates in 3D
    - Via script: when I want to fire the source, I set the Fixed Signal "orientation" by way of script, then fire the impulse.

    I know this is a bit lame to ask before trying, but things have gotten so feature-dense that I know find it's very hard to know when to DIY versus use existing functionality. So is that above the right way to do it, or is there a smarter, built in way for Cinemachine to read the orientation of an Impulse Source?

    TIA!
     
  2. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    237
    Progress here...

    Set everything up. Now, trying to generate the impulse relative to the "facing" of my character...

    Code (CSharp):
    1. impulse.GenerateImpulse(_transform.forward * 10f);
    ... does not even remotely work, the camera shifts as if it uses maybe "Y" as "forward" is that possible? My character (as it should, uses Z as forward). What am I missing?

    Notes:
    - Raw Signal: Fixed Noise, with X and Z values set to a descending linear curve 1 --> 0
    - Amp and Gain: 1
    - Direction Mode: Fixed (my thinking is that I don't want it to slightly "turn towards" the signal, I want to dictate the direction entirely through the GenerateImpulse call... is that wrong?)
    - Scale w Impact: True (just using the default)
    - Use 2D Distance: False (it's a 3d game)

    Totally stuck here, the docs are too abstract, and I can't seem to find a descent example. Again, thanks for any help.
     
  3. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    237
    Gave up on the impulse system and just built something custom.

    IF there is a way to do this with impulse please let me know.
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Have a look at our example scene: ImpulseWave.

    Click play.
    Then click on the Epicenter gameobject. This has CinemachineImpulseSource script attached. Play around with the parameters until you find what you'd like to have. Use the Invoke button to test.

    I think you'd like something like this:
    ImpulseSource:
    Impulse.png
    ImpulseListener:
    ImpulseListener.png
    NoiseDefinition:
    NoiseDefinition.png


    In addition to the above, you'll also need to calculate the direction of the force and set it to the
    CinemachineImpulseSource default velocity:
    For example:
    Code (CSharp):
    1. source.m_DefaultVelocity = (player.transform.position - enemy.transform.position).normalized;
     
  5. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    237
    @gaborkb -- I swear I did all this... :| but I'll give it another go and let you know how I make out. Thanks!
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    The key thing is to have the principal movement along the Y axis of the impulse signal. Then, when you call one of the CinemachineImpulseSource.GenerateImpulse() methods, choose one that allows you to specify a velocity. Set the velocity parameter to be in the direction in which you want the shake to happen. The impulse signal will be rotated automatically such that its local Y axis will be in the direction of the generated impulse's velocity.
     
  7. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    237
    Thanks @Gregoryl -- I'll give this a go soon!
     
  8. bwaite87

    bwaite87

    Joined:
    Nov 23, 2017
    Posts:
    17
    Holy crap, thank you SO much for this response!! I've spent hours experimenting and searching online to get an answer to this. This worked perfectly for me. Same situation as OP, except sidescroller instead of top down
     
    gaborkb likes this.