Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by user-9668 (google) · Feb 28, 2011 at 08:46 PM · animationgunreload

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 28, 2011 at 09:34 PM 0
Share

Your problem sounds like a pure visual thing. Do you have animated your model(weapon/hand/clip) to do a reload?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

  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.

}

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 01, 2011 at 12:02 PM 0
Share

You can also see many other questions that deal with this question: http://answers.unity3d.com/search?q=reload

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges