How To Make A Reload Animation In Unity
Implementing a Reload Animation in Unity!
I recently implemented this reload blitheness to the First Person Shooter project I've been working on for the GameDevHQ course. Essentially, this consists of an Blitheness (made in unity), an Animator Controller, and a script aligning so that the 'R' central triggers the blitheness.
Animation
With an object with an Animator component selected (in my case, the gun itself), we can head into the Animation window and click to 'Create [a] New Prune'. It'due south virtually likely going to exist easiest to set this animation up in Play mode, so I'd recommend hitting Play and locking the Animation window using the padlock icon (this'll foreclose the window from changing if yous select another object).
If we press the record push, we can prepare the keyframes for all of the objects in our animation (in this case, the HandPosition objects we created previously and the Gun model itself). We might besides desire to prepare the position of the pinnacle of our gun and the clip in this case, as this'll assistance us properly recreate the mechanics of reloading a pistol.
This could take some time and finessing, only the main affair is to start by setting our start and end keyframes, and then working on the middle of our animation clip to position the gun and hands accordingly. For the prune-reloading event, this is really simply the aforementioned clip being moved out and back into the gun (creating the illusion of loading a new clip). The summit of the gun can also motility out whilst we're reloading the gun, so slot back into place equally before long every bit the clip is loaded.
I personally needed to add in quite a few extra keyframes, particularly on the hands, but to go them to motion a flake more than convincingly, but feel free to return to this pace afterwards if you'd prefer to tweak it once information technology's all set up up.
Animator Controller
For the Animator Controller, nosotros'll want to drag in our newly-created Reload animation into the Animator window in lodge to create an Animation State. We'll now need to create some transitions to and from this state, returning to our 'Idle'. Every bit a condition on the transition leading into this Reload land, add in a trigger called "Reload". Y'all'll also likely want to disable 'Has Exit Fourth dimension' on this, just so that the player can immediately trigger this animation as long as they're in Idle.
All that'due south left after this is to actually call this Reload trigger from a script 😉
Script
Feel free to add together this code to whichever script you feel it should most appropriately be in; all we demand is a reference to the Animator component the Animator Controller's on (in this case, the gun itself), and an Update method to add together the following:
With this all done, head dorsum into Unity, let it compile, and ensure this Animator variable is assigned in the Inspector. You should at present be able to load up the game in Play style and press R to trigger your new reload animation 😉
Source: https://m-ansley.medium.com/implementing-a-reload-animation-in-unity-c84d874f39c4
Posted by: griffingcosertrut.blogspot.com
0 Response to "How To Make A Reload Animation In Unity"
Post a Comment