Weapon Recoil

Hello everybody recently I’ve been working on a FPS game and today I’ve been stuck on how to add recoil to it.

My original idea was to basically add rotation values to the camera but then I realized the camera’s rotation is always set to something by the controller. The controller I am using is the Unity RigidBody fps controller by the way.

Then I thought about somehow adding some values to the controller for example once the weapon fires it can call a function within the controller that just multiples the “InputX, and InputY” of the mouse inputs by a certain number to maybe offset the rotation of the camera but I am not sure if that is the best way to do it? And since then I have no idea on how I can go further to add recoil.

By the way all the recoil has to do is get two variables from the weapon script which are both floats and just offset the camera to look up or to the left to snap it upwards and the player has to counteract this movement and pull down.

Thanks a lot its a lot to read but I hope you understand the community has been helping me out a lot recently and thanks to that I’ve started to learn slowly but surely thanks guys once again.

I made a simple single-shot recoil sequencer that might be helpful to you, even if it’s just a starting point.

Full package attached below as well.

NOTE: this doesn’t use coroutines because to me it makes it easier to chain on and vary behavior in more complicated ways, such as converting this to be multi-shot recoil a-la some pattern system like CS:GO or pretty much any popular machinegun shooter.

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RecoilOnClick : MonoBehaviour
{
    [Header( "Make the curve domains from 0 to 1.")]
    [Header( "Output value is degrees of deflection.")]
    [Header( "Adjust time interval separately below.")]
    public AnimationCurve RecoilUp;
    public AnimationCurve RecoilRight;

    [Header( "How long is entire recoil sequence?")]
    public float TimeInterval = 0.25f;

    [Header( "Which object is having its .localRotation driven.")]
    public Transform RecoilPivot;

    float recoiling;

    void DriveRecoil(float fraction)
    {
        float up = RecoilUp.Evaluate(fraction);
        float right = RecoilRight.Evaluate(fraction);

        // special number to FORCE you to have zero output when fraction is zero,
        // to keep you from making borked curves and wondering WTF.
        if (fraction == 0)
        {
            up = 0;
            right = 0;
        }

        up = -up;

        RecoilPivot.localRotation = Quaternion.Euler(up, right, 0);
    }

    void Update ()
    {
        if (recoiling == 0)
        {
            if (Input.GetMouseButtonDown(0))
            {
                recoiling = Time.deltaTime;
            }
        }

        if (recoiling > 0)
        {
            float fraction = recoiling / TimeInterval;

            recoiling += Time.deltaTime;
            if (recoiling > TimeInterval)
            {
                recoiling = 0;
                fraction = 0;            // return to time = 0
            }

            DriveRecoil(fraction);
        }
    }
}

6905579–808691–Recoil.unitypackage (218 KB)

Thanks man I will have a look into it I apreciate that.

1 Like

Hey once again I tried your script and also tried to somehow implant it into my game but It wasn’t having it and also I want the recoil to just climb never come down if you know what I mean by that can you give me an idea on how I could set it up to just move it up and the player can pull it down to counter act it thanks a lot.

Ah, the script above has an actual recoil pivot… you probably don’t want to do that and instead want to “inject” the recoil up force into your normal mouse movement processing.

I would still make it a small sharp curve rather than just a single frame of going up… see how I use AnimationCurve objects in the above script? You could use that to blend some “up” force into the normal mouse look you’re using in the moments after a shot is fired. Then the user would be required to pull it down to counter and get the sights back on target.

1 Like

Hey man I know its been a while but do you have any ideas on how I could add a rotation into the camera for example I could add like 5 degrees into a axis to make it point upwards. I know in Unreal engine its done by adding controller input but I’m not sure about unity thanks.

That’s just a local rotation of a transform. Get familiar with how transforms and rotations parent to each other. It’s not really something I can type at you. Just do some tutorials, pivot some stuff, fiddle with it and see how “5 degrees” gets deflected as part of a hierarchy of Transforms.

Thanks for all of your help once again you came in clutch. I did some research and fiddled around with the rotation and got it working I was even able to add leaning and other movement stuff that I wanted to add as well as recoil I just went ahead and injected it like you said earlier into my mouse controller.

what do i put for the recoil pivoit and the line things

See above.