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

Reload Gun script

I want to make a reload script for FPS game t$$anonymous$$s 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 t$$anonymous$$s in script

i've done every t$$anonymous$$ng 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 t$$anonymous$$s 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
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 · Oct 28, 2010 at 11:45 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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
Add comment · 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
avatar image audicrafterhd · Oct 01, 2017 at 09:24 AM 0
Share

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 gun? simpel question 2 Answers

Function broken after Application.LoadLevel() 2 Answers

Gun with ammo help 1 Answer

Reload Script Not Working 1 Answer

Prefabs missing on reload 0 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