- Home /
reload script does not work when ammo is 16
so I have the most strange script ever made. it works like a charm but when clip is 1 or 2 and totalbullets is 16 the script won't work it's soooo strange can someone tell me what might be the problem
here's the script
var ReloadTime = 1;
var Nifes = 3;
var SpreadFactor : float = 0;
var AimSpread : float = 0.01;
var HipFireSpread : float = 0.01;
var AimCamera : GameObject;
var BulletHole : GameObject;
var Nife : GameObject;
var NifeSpawn : GameObject;
var IsReloading = false;
var GunFireSound : AudioClip;
var EmptyGunSound : AudioClip;
var Gun : GameObject;
var GunReloadSound : AudioClip;
var AnimationPlayed = false;
var MuzzleFlash : Renderer;
function Start() {
GunReloadAnimation = Gun.GetComponent.<Animator>();
MuzzleFlash.enabled = false;
}
function Update() {
AimCamera.GetComponent.<Crosshair>().enabled = true;
SpreadFactor = HipFireSpread;
if(Input.GetButton("Fire2")){
AimCamera.GetComponent.<Crosshair>().enabled = false;
SpreadFactor = AimSpread;
}
if(Input.GetButton("Fire1") && Time.time > NextFire && Clip > 0){
NextFire = Time.time + FireRate;
GetComponent.<AudioSource>().clip = GunFireSound;
GetComponent.<AudioSource>().time = 0.2f;
GetComponent.<AudioSource>().Play();
Rayshooting();
Clip --;
}
else if(Clip == 0 && TotalBullets != 0){
GetComponent.<AudioSource>().time = 0.4f;
GetComponent.<AudioSource>().clip = GunReloadSound;
GetComponent.<AudioSource>().Play();
Reload();
}
else if(Input.GetButtonDown("Fire1") && Clip == 0 && TotalBullets == 0){
EmptySound();
}
if(Input.GetKey(KeyCode.R) && Clip < 30 && TotalBullets != 0 && IsReloading == false) {
GetComponent.<AudioSource>().time = 0.4f;
GetComponent.<AudioSource>().clip = GunReloadSound;
GetComponent.<AudioSource>().Play();
Reload();
}
if(Input.GetKeyDown(KeyCode.E) && Nifes > 0){
Instantiate(Nife, NifeSpawn.transform.position, NifeSpawn.transform.rotation);
Nifes --;
}
}
function Rayshooting() {
{
MuzzleFlash.GetComponent.<Renderer>().enabled = true;
yield WaitForSeconds(0.02);
MuzzleFlash.GetComponent.<Renderer>().enabled = false;
}
var direction : Vector3 = transform.forward;
direction.x += Random.Range(-SpreadFactor, SpreadFactor);
direction.y += Random.Range(-SpreadFactor, SpreadFactor);
direction.z += Random.Range(-SpreadFactor, SpreadFactor);
Debug.DrawRay(transform.position,direction * BulletRange);
var hit : RaycastHit;
bHit = Physics.Raycast(transform.position,direction,hit,BulletRange);
if (bHit && hit.transform.gameObject.tag == "Enemy"){
hit.transform.SendMessage("DamageTaken", Damage, SendMessageOptions.DontRequireReceiver);
}
else if (bHit && hit.transform.gameObject.tag == "Buildings" || "Buildings2"){
var CloneHole = Instantiate(BulletHole, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(CloneHole, 5);
}
}
function Reload() {
if(Clip == 0 && TotalBullets >= 30){
Clip = 30;
TotalBullets -= 30;
Debug.Log("Did reload 1");
}
if(Clip == 0 && TotalBullets << 30){
Clip = TotalBullets;
TotalBullets = TotalBullets - TotalBullets;
Debug.Log("Did reload 2");
}
if(Clip != 0 && TotalBullets >= 30 - Clip){
TotalBullets = TotalBullets - (30 - Clip);
Clip = 30;
Debug.Log("Did reload 3");
}
else if(Clip != 0 && TotalBullets << 30 - Clip){
Clip = Clip + TotalBullets;
TotalBullets = 0;
Debug.Log("Did reload 4");
}
}
function EmptySound(){
GetComponent.<AudioSource>().time = 0.0f;
GetComponent.<AudioSource>().volume = 1;
GetComponent.<AudioSource>().clip = EmptyGunSound;
GetComponent.<AudioSource>().Play();
}
Hi, it is look like that you have problem in you if statement on Reload() function. Try to use just "if" on line 112 ins$$anonymous$$d "else if"
Yupp I figured that out too and fixed it the Same day as I posted this question
Answer by ScaniX · Aug 03, 2016 at 08:47 AM
You have some extraordinary code lines in there:
if(Clip == 0 && TotalBullets << 30){
I guess that comparison should be like this instead (at both places):
TotalBullets < 30
Otherwise you are doing some serious bitshifting here.
If you can just fill up the clip with bullets from your TotalBullets, then you should be able to make it easier like this:
if (Clip < 30 && TotalBullets > 0) {
int refill = Math.Min(30 - Clip, TotalBullets);
TotalBullets -= refill;
Clip += refill;
Debug.Log("Refilled clip with " + refill + " bullets, bullets remaining: " + TotalBullets);
}
Well I used << I seem to get errors if I don't sometimes. But I think 1 is enough then i'll change that The code worked tho I don't Have a clue First what was written but a quick look in the wiki gave me the information I needed It is basically the Same one as used by initram but the part where I don't use constants is why I will accept this script and close the thread
Answer by initram · Aug 03, 2016 at 01:12 PM
There is no need for all those conditionals. Math is both simpler and faster. This code should cover all your cases.
const maxBulletsInClip = 30;
function Reload()
{
var missingBullets = maxBulletsInClip - Clip;
var bulletsToMove = Math.min(missingBullets, TotalBullets);
Clip += bulletsToMove;
TotalBullets -= bulletsToMove;
}
The reason why your code does not work is most likely because you use "left shift" instead of "less than" see here for documentation https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Left_shift
You should write < instead of <<
I thank you for this script but I cannot use it as of Right now because by using constant i'm not able to do what I want in the current State of my project it however indeed is way cleaner than my code but im a Total noob learning hard lol only 15 years old
Your answer
Follow this Question
Related Questions
1 script doesn't react to input 1 Answer
Reload ammo with Clips using variable Substraction 1 Answer
reloading script problem 0 Answers
How To Disable/Enable Function 2 Answers