Hi guys. I did this script but the variable “ammo” don’t change and it is ever = 30. (sorry for bad english xD) I don’t get errors in the console…
var ammo : int = 30;
function Update ()
{
DecreseAmmo();
print(ammo);
}
function DecreseAmmo()
{
if(Input.GetKeyDown(0))
{
for (var k = 30; k == 0; k--)
{
ammo -= 1;
}
yield WaitForSeconds(0.125);
}
}
Left righty : yes i want it to decrease ammo by 1 every 0.125f secs (1 every bullet in fire rate 8 every 1 sec)
Yes I tried to put in the update but it don’ t work D: ! WAIT ! But the KeyDown is the left click of the mouse??
ah yeah, what Sly said… I just copy/pasted chunks from the example code above as an example of the kind of logic loop needed… I should probably keep “real code” looking snippets out of pseudo code in future
it works but instead of subtracting the variable ammo 1 it subtracts 60/80 at a time. I don’t know. I do not know. But maybe there is some problem in the other scripts in this package that I downloaded from the assets store.
well your script says when u click the mousebutton it will decrease 30 ammo (due to the for loop) and then wait .125 seconds and does the same… is this what u wanted it to do?
var ammo : int = 30;
function Update ()
{
DecreseAmmo();
print(ammo);
}
function DecreseAmmo()
{
if(Input.GetMouseButtonDown(0))
{
for (var k = 30; k > 0; k--)
{
ammo -= 1;
}
yield WaitForSeconds(0.125);
}
}
Then you need to change some stuff within your script.
something like that
var ammo : int = 30;
var waitTime : bool = false;
function Update ()
{
DecreseAmmo();
print(ammo);
}
function DecreseAmmo()
{
if(Input.GetMouseButtonDown(0))
{
// do we have to wait before next ammo reduction?
if(!waitTime ammo > 0)
{
ammo -= 1;
}
// wait for x seconds and then activate our function again so we only remove 1 ammo every 0.125 seconds
yield WaitForSeconds(0.125f);
waitTime = true;
}
}
It might be that the waitforseconds thing wont work, so you have to use Time.time instead to compare the seconds u need to wait before the next ammo will be reduced
It works but it reduces only 1 ammo and then does not reduce more. Maybe when I reduced 1 ammo waitTime must return the variable to false.
This is a big problem…
Sorry my mistake, typed this a bit fast and beside doing something different, this should work now:
var ammo : int = 30;
var waitTime : bool = false;
function Update ()
{
DecreseAmmo();
print(ammo);
}
function DecreseAmmo()
{
if(Input.GetMouseButtonDown(0))
{
// do we have to wait before next ammo reduction?
if(!waitTime ammo > 0)
{
ammo -= 1;
waitTime = true;
}
// wait for x seconds and then activate our function again so we only remove 1 ammo every 0.125 seconds
yield WaitForSeconds(0.125f);
waitTime = false;
}
}