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 pdh1kid · Nov 13, 2010 at 01:47 PM · fpsgun

Gun Ammo help in JS

This is my first post and I need help with getting a ammo count that will decrease every time a bullet is fired. I would aslo like to get a reload function that would sutract from a clipsLeft var and reset the ammo var. this is what I have so far.

var projectile : Transform; var shootSpawn : Transform; var ammo : int = 6; var clipsLeft : int = 10;</p> <p>function Update () { if (Input.GetButtonDown("Fire1")){ if (Ammo > 0){ clone = Instantiate(projectile, shootSpawn.position, shootSpawn.rotation); Physics.IgnoreCollision(clone.collider, collider); //This is the subtraction function that I'm having trouble with. ammo -= 1; } else { print("Reload"); } } }

If anyone could answer using Java Script it would be much appreciated!!!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Tom 10 · Nov 13, 2010 at 09:30 PM

Does this do what you're looking for?

var projectile:Transform; var shootSpawn:Transform; var clipsLeft:int = 10; // how many clips the player has var clipSize:int = 6; // how many bullets in 1 clip private var ammo:int; // how many bullets left in the current clip

function Start() { ammo = clipSize; // Start with this many bullets. }

function Update() { if(Input.GetButtonDown("Fire1")) { if(ammo > 0) { var clone:Transform = Instantiate(projectile, shootSpawn.position, shootSpawn.rotation); Physics.IgnoreCollision(clone.collider, collider); ammo--; // Subtract 1 bullet if(ammo <= 0) Reload(); // Do you want to call reload here... } else { Reload(); // ...or here? } } }

function Reload() { if(clipsLeft > 0) { clipsLeft--; ammo = clipSize; // Play sound/animation ? } else { // No more clips left... } }

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 pdh1kid · Nov 14, 2010 at 02:19 PM 0
Share

For the most part yes! thank you!

avatar image mlmihjaz · Sep 25, 2013 at 04:15 PM 0
Share

can i have a tutorial such that multiple random enemies environment a single character should move randomly and then if collision detected they will start to fire..but i have to code to one object only it will come as many object with random movements..

Please help..or give a link.....

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

1 Person is following this question.

avatar image

Related Questions

How to shoot RayCast in center of screen? 1 Answer

Raycast shooting in the middle of the screen 1 Answer

Aim Down Sights and Weapon Sway not working unless animations are not assigned 0 Answers

damage system 0 Answers

Aiming down a gun 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