Hi, I’ve got a script for the amount of ammo for my character, but when he’s reloading in the script can i shoot in the game. I want my character not to shoot when reloading. Can someone help me? here’s my script:
// It needs to know how many bullets (rounds) one clip can hold
var magazineCapacity = 30;
// It needs to know how many rounds are left in the current magazine (clip)
var remainingRounds = 30;
// Then setup your `Fire` function
public function Fire ()
{
if (remainingRounds > 0) { // we still have bullets left, fire away!
// ...
// Instantiate your bullets/missiles/plasma blasts/teddy bears here
// ...
remainingRounds -= 1; // remove that bullet from the magazine
} else { // we're out
// ...
// play the classic "click, click" out of ammo sound
// or however you want to notify the user they need to reload
// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5)
// Prints 5.0
print (Time.time);
// ...
Reload();
// if the user forgets to reload go ahead and do it
// for them when they try to shoot again
}
}
// Then attach an input to that function
public function Update ()
{
// ...
if ( Input.GetButtonDown( "Jump" ) ) { Fire(); }
if ( Input.GetButtonDown( "reload" ) ) { Reload(); }
// ...
}
// Then you'll need that `Reload` function we used a minute ago
public function Reload ()
{
// ... trigger the reload animation
remainingRounds = magazineCapacity; // put in a new clip
}
and there is another thing how do i say in the script that when i press the ‘r’ button he’s going to reload?
I generally use something like that but i don’t know will it work on your script, do something like that:
var a = 0;
if(a==0){
if (remainingRounds > 0) { // we still have bullets left, fire away!
// ...
// Instantiate your bullets/missiles/plasma blasts/teddy bears here
// ...
remainingRounds -= 1; // remove that bullet from the magazine
} else { // we're out
// ...
// play the classic "click, click" out of ammo sound
// or however you want to notify the user they need to reload
// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5)
// Prints 5.0
print (Time.time);
// ...
Reload();
// if the user forgets to reload go ahead and do it
// for them when they try to shoot again
}
}
And in your Reload function, before the anim start just say a=1; , which means you wont be able to shoot because you need a to be 0 to shoot, and after the animation code just put a=0; to make it shootable ( lets say ) again.
Edit : And i see you are not using any animation code, so you can do the same thing to
this :
remainingRounds = magazineCapacity;
it doesn’t matter, even you can do something like that ;
a=1; // we are not able to fire
yield WaitForSeconds(2); // the reload takes 2 seconds to fire
a=0; // we can shoot again
And also for that R button thing : If(Input.GetKeyDown("r")){ Reload(); }
And also for that R button thing : If(Input.GetKeyDown("r")){ Reload(); }
– Inan-Evin