- Home /
How to add the needed amount to the AmmoCount (current ammo)?
So I'm creating an ammo script for an AK47. The clip holds 30 bullets, and the player can hold up to 4 clips (120 bullets) at one time. I would like to make the script deduct the necessary integers from the TotalAmmo variable. I know that it is something like AmmoCount = AmmoCount + something;, but I can't seem to figure out what the "something" is. I guess I need a refresher in math :P. Basically, I want to ask Unity to find X, and X is the number that adds up to the clip. Find X, and we have our script.
Please excuse my messy script, as I am a tad new. I've been coding for about 2 years now, here and there, and I need much practice.
BTW this is also my fire script, and part of my animation script (I use the CanFire variable to determine if the animation can play or not. So if you can't fire, like if ammo is 0, then it won't play the firing animation.)
Anyone willing to help? Greatly appreciated... --Emsiardy
var speed : float = 60;
var SoundLocation : GameObject;
var pickup : AudioClip;
var projectile : GameObject;
var fireRate : float = 0.09;
var reloadRate : float = 0.01;
var AmmoGUI : GUIText;
var AmmoCount : float = 30;
var Clip : float = 30;
var TotalAmmo : float = 120;
var CanReload : boolean = false;
var CanFire : boolean = true;
private var nextFire : float = 0.0;
private var nextReload : float = 0.0;
var MathProblemHere; // Just to remind myself where to fix the reload int
function Update () {
if(Input.GetButton ("Fire1") && Time.time > nextFire && CanFire == true) {
nextFire = Time.time + fireRate;
AmmoCount --;
var clone : GameObject =
Instantiate(projectile, transform.position, transform.rotation) as GameObject;
AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position);
clone.rigidbody.velocity = transform.TransformDirection( Vector3 (0, speed, 0));
Destroy(clone, 9);
}
if(Input.GetButtonDown("Reload") && CanReload == true && AmmoCount < TotalAmmo && TotalAmmo > 0 && Time.time > nextReload) {
nextReload = Time.time + reloadRate;
AmmoCount = AmmoCount + MathProblemHere;
//deduct from TotalAmmo and add to AmmoCount the necessary integer here
}
if(AmmoCount <= 0) {
AmmoCount = 0;
CanFire = false;
}
if(AmmoCount > 0) {
CanFire = true;
}
if(AmmoCount >= Clip) {
AmmoCount = Clip;
CanReload = false;
}
if(AmmoCount < Clip) {
CanReload = true;
}
}
function OnGUI() {
AmmoGUI.text = "Ammo: " + AmmoCount + " | " + TotalAmmo.ToString();
AmmoGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
}
Answer by Adamcbrz · Apr 22, 2014 at 04:36 AM
Your code could be something similar to the following:
var loadingClip = Mathf.Min(Clip, TotalAmmo)-AmmoCount;
TotalAmmo -= loadingClip;
AmmoCount += loadingClip;
I think that will do what your asking.
This code doesn't really do much, how would you suggest implementing it?
What about this?
var speed : float = 60;
var SoundLocation : GameObject;
var pickup : AudioClip;
var projectile : GameObject;
var fireRate : float = 0.09;
var reloadRate : float = 0.01;
var AmmoGUI : GUIText;
var AmmoCount : float = 30;
var Clip : float = 30;
var TotalAmmo : float = 120;
var CanReload : boolean = false;
var CanFire : boolean = true;
var loadingClip = $$anonymous$$athf.$$anonymous$$in(Clip, TotalAmmo) - AmmoCount;
private var nextFire : float = 0.0;
private var nextReload : float = 0.0;
function Update () {
if(Input.GetButton ("Fire1") && Time.time > nextFire && CanFire == true) {
nextFire = Time.time + fireRate;
AmmoCount --;
var clone : GameObject =
Instantiate(projectile, transform.position, transform.rotation) as GameObject;
AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position);
clone.rigidbody.velocity = transform.TransformDirection( Vector3 (0, speed, 0));
Destroy(clone, 9);
}
if(Input.Get$$anonymous$$eyDown("r") && CanReload == true && AmmoCount < TotalAmmo && TotalAmmo > 0 && Time.time > nextReload) {
nextReload = Time.time + reloadRate;
TotalAmmo -= loadingClip;
AmmoCount += loadingClip;
//deduct from TotalAmmo and add to AmmoCount the necessary integer here
}
if(AmmoCount <= 0) {
AmmoCount = 0;
CanFire = false;
}
if(AmmoCount > 0) {
CanFire = true;
}
if(AmmoCount >= Clip) {
AmmoCount = Clip;
CanReload = false;
}
if(AmmoCount < Clip) {
CanReload = true;
}
}
function OnGUI() {
AmmoGUI.text = "Ammo: " + AmmoCount + " | " + TotalAmmo.ToString();
AmmoGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
}
From you question it sounds like all you are looking for is the math to find the number of bullets to load. This would go into your reload logic. Is there something I am missing in your question?
No you're right.. I guess I was just looking for a quick fix!!! Ha. Thanks though! This got me on the road to eventually get where I'm going.
But wait.. doesn't $$anonymous$$ath.$$anonymous$$ just find the smallest number??? Like, between 1 and 5, it would just return 1 wouldn't it???
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Shooting speed help! 1 Answer
Ammo with ammobar problem. 0 Answers
C# Reload Script 1 Answer
Buying Ammo HELP 0 Answers