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

Question Camera recoil on FreeLook camera

Discussion in 'Cinemachine' started by hawaiian_lasagne, Jun 9, 2020.

  1. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    I'm trying to implement muzzle-climb style camera recoil using cinemachine free-look camera and impulse source.

    My noise settings contains -20 degrees for RotationX, and it kinda works.

    Though there are two issues:
    1) The camera returns back to the original position when no more calls to GenerateImpulse are made.
    2) The camera rotates around it's local axis, rather than around the CameraLookAt pivot point.

    Is there an easy method to fix these issues? If not, would it be possible to write a cinemachine extension to achieve this effect?

    Thanks
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You can write a custom extension to get what you want (I don't know exactly what "muzzle-climb style camera recoil" is, but if it involves displacement and/or rotation of the camera then you can probably do it).

    In your custom extension, implement PostPipelineStageCallback():
    Code (CSharp):
    1.     protected override void PostPipelineStageCallback(
    2.         CinemachineVirtualCameraBase vcam,
    3.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    4.     {
    5.         if (stage == CinemachineCore.Stage.Aim)
    6.         {
    7.             state.PositionCorrection += ... // camera position offset
    8.             state.OrientationCorrection = ... // local camera rotation
    9.  
    10.             // state.ReferenceLookAt is the lookAt point, if you need it
    11.         }
    12.     }
     
    hawaiian_lasagne likes this.
  3. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    Thanks Gregoryl

    Muzzle climb is just when the weapon recoils upwards with each shot, until you're eventually looking at the sky.

    Id like to simulate the same behaviour the mouse has on a freelook camera, where it blends between each rig, as the recoil happens. If I write directly to the PositionCorrection then it doesn't blend between the rigs correctly, it's applied afterwards and on top.

    Does that make sense? Maybe there is some way I could manipulate the mouse input being fed into cinemachine instead?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    If you just want to move the vertical axis then you can directly increment FreeLook.m_YAxis.Value
     
  5. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    Ok that sort of works. Except now I loose the original position before the recoil. I'd like this to be additive so I can blend back to zero easily.

    Is there another method I could use to change the camera position but have it blend between the rigs correctly?
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Your script that's modifying the Y axis value can keep track of what it adds, and then remove it afterwards.
    As for other methods, an extension put on the FreeLook will apply its effect to all 3 rigs.
     
  7. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    Thank you, I'll have a play around with the extensions. I think what I'm wanting to control is the camera's position along the spline that connects the three rigs.

    I'm currently modifying the mouse axis value directly to get the muzzle climb effect, and then using the impulse generator to add some noise. Starting to look pretty good.

    Sorry I know I am pushing it now, but I'm wondering if there is a way to keep my player mostly still on screen, and have just the environment shake around me.

    Thank you for your help!
     
    Last edited: Jun 9, 2020