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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Weapon Model Recoil

Discussion in 'Scripting' started by M1_35, Jul 14, 2022.

  1. M1_35

    M1_35

    Joined:
    Feb 27, 2021
    Posts:
    3
    Hi, I got stuck demo-ing my shooter game. I don't know how to implement model recoil properly.
    It isn't impossible, I just can't figure it out.

    The things I'm looking for are slide/bolt animations and continuous recoil.
    I tried a few approaches,

    1. Using a 'shoot' animation only. Problems: The animations would get out of sync after automatic fire. Had to change entire animation to change fire rate.

    2. Using a recoil script. This was the closest to how I wanted it, and easiest to work with. Problems: No slide animations. Bolt action rifles weren't animated.

    3. Perfectly timing an animation to play with the recoil script. Problems: Inconvenient for animator to animate a slide/bolt animation without recoil, and to time it perfectly to the fire-rate. The animations would get out of sync after prolonged fire.

    4. Using blend-trees to move guns instead. This one was basically the recoil script, but without being able to change values easily.

    I looked at numerous tutorials and websites, and could not figure out how games achieved this. For example, CS:GO. It has bolt animations and recoil for the snipers and shotguns, but also continuous recoil for the automatic weapons. Thanks in advance.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Don't mix gameplay elements with visual elements. When your gun recoils, you modify the rig's transform that contains your weapon model, not the weapon model / animation state itself.
     
    M1_35 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    There's also two separate components here:

    - upon firing a weapon physically recoils, potentially putting it in a different gameplay location for the subsequent shot... this is a transform translate upwards and then back downwards to where it originally was, or to a slightly different spot (entirely dependent on game design)

    - the actual bolt action mechanism you describe, which is simply a function of the operation of the gun mechanism: extract, eject, load and bring the fresh round into battery

    Both start at the same time but are completely independent. Barring a misfeed, the gun mechanism should return fully to battery, but the muzzle might now be pointed up a bit or to one side.

    See how CS:GO does it... they incorporate it into a skill-based thing you get good at by playing, plus its different for every gun time. Rivers of digital ink have been spilled describing how CS:GO works and how to learn how to shoot properly. Or pick the game you're trying to copy and go look for discussions of what they do. Every detail could make a difference.
     
    M1_35 likes this.
  4. M1_35

    M1_35

    Joined:
    Feb 27, 2021
    Posts:
    3
    Yes, this is the part I've had no problem with. I coded a little script that accomplishes it nice for me.
    So, the solution is to use a script along with animation? I tried that, and the animation was weird to compensate for scripted recoil, basically a static gun with the slide moving back, but it worked well enough. The only issue I had was for animations that were longer than a few frames, and not at the same time as the shot, like the pump of a shotgun or the bolt of a sniper. When I did that, I had to either input a float telling the script when it can fire again, (to avoid the animation not completing, but being able to fire), or to add an animation event telling the script that it finished. I can use this one, it just felt unnecessarily complicated.

    Sorry if I'm not being clear. This happens to be my first post.

    Edit: In CS:GO it appears to be an animation. I still don't know how they combined continuous recoil animations with slide animations, like with the assault rifles.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Easy: the animation moves the player's hand (perhaps through IK or just an offset?) and the slide is just a local animated visual component that doesn't affect gameplay.
     
    M1_35 likes this.
  6. M1_35

    M1_35

    Joined:
    Feb 27, 2021
    Posts:
    3
    By "local animated visual component", do you mean a 'shoot' animation in the gun animator? I think I'll try that again, thanks. A hybrid approach should solve the bolt animations and such. My last question is how I'd keep the animation from overlapping. Eg, if the slide takes 0.05s to reset and the fire-rate is 0.04s. I could just have all the fire-rates capped so it doesn't take longer than the slide, but I don't know if there's a better way.

    Edit: I'm not waiting for the animation to finish for the fire-rate. It would be an entirely visual problem. I promise I'm not normally this clueless, animations are the only thing I will never understand.
     
    Last edited: Jul 15, 2022