# Should recoil be an animation or script?

Should recoil for a rapid fire gun be be done through an animation or a script? I want the recoil to increase as I continuously fire, like a lot of FPS games do.

I was initially considering achieving this through a script in unity that rotates the gun slightly each time I fire, but I realized that since I plan to have most rapid fire guns be 2 handed weapons, at some point, the left hand won't be on the weapon.

However, since I also want the recoil to increase as I fire, I'm not sure how I can achieve this in an animation.

How is recoil typically done? Input on this would be very much appreciated.

This should be done via script.

How much recoil? Let's say 10 degrees at a rate of 5 m/s.

Now you level up your handling, and recoil is reduced by 10%. So now our recoil is 9 (10 * 0.9) degrees at a rate of 4.5 (5 * 0.9) m/s. This is a much easier to do and change via a script than it is via animation.

Please see Phillip's answer as well. He goes into more detail, and talks about using the animation system as well. (Although I still think doing this via scripting is the best approach.)

• Do you know how I can keep the left hand on the gun, as I plan to have most rapid fire weapons be 2 handed weapons? I too thought it might be better via script, but realized I didn't know how to keep the hands on the gun.
– KI.
Commented Nov 9, 2020 at 17:07
• @KI. That depends. I suggest you form a new question with details specific to your project. 2D or 3D? How is your rigging set up? What code do you have so far? How does your character look in the hierarchy? Etc. (Whatever you post, we will probably ask for more details in the comments.)
– Evorlor
Commented Nov 9, 2020 at 17:11
• @KI. If you really want to "glue" the hands to the gun so they follow it wherever it goes, then you might want to look into inverse kinematics. But most 3d modelers would make gun and hands part of the same rig, so they wouldn't have that problem. Commented Nov 9, 2020 at 18:04
• @Philipp That does seem to be the easier option, which I did consider, but I also came across this thread that suggested the opposite. <forums.unrealengine.com/development-discussion/content-creation/…>
– KI.
Commented Nov 9, 2020 at 18:18
• @KI. This thread is about a slightly different question: Making gun and arms part of the same model. I suggested to make them part of the same rig. Also, looking over that thread, people seem to have all kinds of opinions supported by all kinds of reasonably sounding arguments. As I wrote in my answer: There is never the best solution to a problem. Just the best solution for you. Commented Nov 9, 2020 at 22:45

First of all, I would really recommend you to separate the projectiles being spawned from the gun weapon model and its animation. Attaching the spawn point to the gun model just causes unnecessary problems and unexpected behavior (like bullets flying into unexpected direction because you are shooting while still blending to the weapon switching animation). Rather attach the spawn point to the root of the character object, so you have better control over where the bullets spawn. The less surprises, the better. And now your animators are free to animate the gun and the character's hands the way they want without having to worry about the animation exactly matching the bullet spread pattern.

But that still does not mean that the recoil pattern can not be controlled through an animation. The Unity animation system is very versatile. It can do more than just control the bones of character models. It can "animate" (almost) any inspector-visible attribute of any component. So it is perfectly possible to create an "animation" for how the point of aim and random spread of a weapon changes over time while the gun is shooting. This allows you to edit the spread patterns of guns by editing curves in the Unity animation curve editor, which can be a very designer-friendly and powerful tool.

A code-based solution might be less intuitive for a designer, but more intuitive to create for a programmer. You might also be more comfortable with designing your recoil patterns as mathematical functions rather than by dragging around curves until they feel right. So that's also an option.

In software development, there is never just one solution for a problem. There is also usually not a best solution. Only the solution which is best for you.

– Evorlor
Commented Nov 9, 2020 at 18:20

# Both - Consider Procedural Animation

For example, you could create an animation that linearly rotates the weapon a specific angle (moving both hands and whatever else you need with it), but not "play" it in its entirety but set at which point in the animation you are via script. Say you have a max. rotation of 90 degrees, you could make the animation "take" 90 seconds and just assign myAnimation.time = kickbackInDegrees. The same can be done with a second animation for left/right kick.

This approach works well for complex and/or cumulative inputs like your cumulative recoil (multiple shots in rapid succession kick more than a single shot).

• So I make the animation play until a certain point? Like if I make the myAnimation.time = 5, the gun rotates 5 degrees? How would the gun going back down work ? This sounds like an interesting approach though.
– KI.
Commented Nov 9, 2020 at 18:27
• The simplest approach would be to set it each frame, e.g. in FixedUpdate do something like recoil *= 0.8f, in Update myAnimation.time = recoil and when the weapon fires, you add your 5 degrees to recoil. Make sense? Commented Nov 9, 2020 at 19:00
• I think I see how this works. This is definitely an approach I hadn't even thought of, thank you for your input.
– KI.
Commented Nov 9, 2020 at 19:10