- Home /
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!!!
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... } }
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
Follow this Question
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