C# animation script for reloading a gun

Hey guys. I making a shooting game with unity 3d, and I want a script or something that makes the gun physically reload.( you know, like take the clip out with your hand, and put in a new one). that sorta thing. So id appreicate if anyone could give me directions on how to do that. thanks so much guys. Please do it in C Sharp (C#) so i can understand because I roughly know what to do. Note: I don't know Javascript and i am not very advanced with C# (I know the basics).

Thank you very much for your further replys.


If you have a reload animation, just play it:

void Reload()
    // And of course do your logical reload.
    // aka: set your bullet count; reduce clip count.

There's maybe some more logic involved but that depends on your animation system.

To make a model "physically reload", you are referring to an animation. You'll need to have an animation ready, or create one, or the very tedious and hard way would be to animate each transform in code. I would strongly, strongly recommend you'd use or create an animation, not coding the animation yourself. While it is possible, it hasn't many benefits at all.

So, to reload the gun you want to do two things.

  1. Play the animation.
  2. Wait for the animation to complete.

The reason we want to wait for it to complete is so we can tell the gun it's now ok to start firing again, and maybe to set the available bullets only after the animation completes.

I suggest you read the two links and then your code could end up something like this:

// This is a co-routine
IEnumerator Reload()
    // Code that should happen just before the reload begin
    // Like play reload sound, or check if isReloading or have any clips.
    // You can set whatever code you like here.

    yield return animation.WhilePlaying("Something");

    // Code that should happen just after the reload ends
    // Like play clip sound, allow player to fire again and 
    // set how many bullets were reloaded.
    // You can set whatever code you like here.


You just can't copy and paste this code to assume it will work. It will not. At the very least you need to see the accepted answer following the link in item #2, Wait for the animation to complete. It deals with how you make code wait for an animation to complete. Otherwise you'd have to approximate this with timers.

Here is a code that works for me for reloading. When you hit the Space bar, a reloading animation will start but its just a animation and not really reloading. And do not try and copy and paste the script ether. Here is is

function Update()
    // Button for reloading gun
        // Searching for animation

Now attach this script to yor ammo pack item and give it a animation called "Reload" and play the game and hit the space bar and watch as your animation plays.