• 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
Question by Mihai · Oct 28, 2010 at 06:36 PM · reload

Reload Gun script

I want to make a reload script for FPS game this way: exp:if the player has 60 bullets left in total and 14 loaded,when he press r button i want to have 44 bullets left and 30 loaded,but i don't know how to put this in script

i've done every thing but i dont know how to make the ecuation

like in call of duty

here it is what i made so far:

var sound : AudioClip; var bulletSpeed = 100; var projectile : Rigidbody; var bullets = 30; var totalBullets = 60; var reloadTime = 3;

function Update () { Fire(); }

function Fire () {

if(bullets > 0) { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation);

     // Give the cloned object an initial velocity along the current 
     // object's Z axis
        clone.velocity = transform.TransformDirection (Vector3.forward * bulletSpeed);
        AudioSource.PlayClipAtPoint(sound, transform.position, 1);
     //take a bullet of our clip if firing       
        bullets -=1;
        GameObject.Find("bullet_count").guiText.text = ""+totalBullets;
 }

}

}

function Reload() { if(Input.GetButtonDown("Reload"))
{ yield WaitForSeconds (reloadTime) ;

 if(totalBullets > 0)
 {

 }

 }

}

Comment

People who like this

0 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 · Oct 28, 2010 at 11:45 PM 0
Share

Edit your post, copy and paste in the code, highlight the code, and press the button with 0s and 1s on it so it's formatted.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by luqui · Oct 29, 2010 at 01:29 PM

Here's the math.

int transfer = 30 - bullets;
if (transfer > totalBullets) { transfer = totalBullets; }
bullets += transfer;
totalBullets -= transfer;

Comment
audicrafterhd

People who like this

1 Show 2 · 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 Mihai · Oct 29, 2010 at 03:17 PM 0
Share

it keep telling me to insert a semicolon at line 50!!!!!!!! (on the int transfer = 30 - bullets;)

avatar image audicrafterhd · Oct 01, 2017 at 09:24 AM 0
Share

Thank you so much dude!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Reload scene when dead 5 Answers

Wait to Reload 2 Answers

Automatic weapon does not stop firing when it runs out of ammo. Using RayCast and Javascript. 2 Answers

need help deleting ammo item in inventory on gun reload 1 Answer

Call method after loading scene? 1 Answer


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