- Home /
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.
Thanks
Your problem sounds like a pure visual thing. Do you have animated your model(weapon/hand/clip) to do a reload?
Answer by Bunny83 · Feb 28, 2011 at 09:38 PM
If you have a reload animation, just play it:
void Reload()
{
animation.CrossFade("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.
Answer by Statement · Mar 01, 2011 at 11:50 AM
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.
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.
}
NOTE!
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.
You can also see many other questions that deal with this question: http://answers.unity3d.com/search?q=reload
Answer by Kashaunzilla · Apr 15, 2011 at 01:08 AM
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
if(Input.GetButtonDown("Jump"))
{
// Searching for animation
animation.Play("Reload");
}
}
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.
Your answer
Follow this Question
Related Questions
arms with gun and animation 1 Answer
Wierd rotations in very simple reload animation 0 Answers
Problem with animations(noob question i guess) 1 Answer
Switch Weapon Script Not taking effect HELP!! 2 Answers
My machine gun code steels my clips 4 Answers