hi all i have a reaload script and i need a timer to set the realoads and be able to shoot if timer has passed
this is reaload code i made it shorter for reading purposes
void Reaload ()
{
if (weaponswitch == false) // if we have primary
{
if (Input.GetButton ("Reaload") & lalalal)
{
do
{
ammoinclip++;
ammo--;
}
while (we have ammo );
}
}
else // this is if we have secondary
{
if (Input.GetButton ("Reaload") & lala)
{
do
{
++;
--;
}
while (we still have ammo );
}
}
}
}
}
the shooting code is basicly if input fire1 instanciate so how can i make a timer ?
A simple way to do what you’re asking would be to set the next available firing time in a variable.
When you fire, set the NextFireTime = Time.time + FiringDelay;
then , when you check for your input:
if (Input.GetButton(fire1) && Time.time >= NextFireTime) {
NextFireTime = Time.time + FiringDelay;
// fire whatever..
}
i mean to reaload a weapon not to shoot it … when i press R (reaload key ) i want it to wait for a a couple of seconds then reaload the gun well actulay to run the code which reloads the gun
Well, you could still use the same idea. Just think of the variables I provided before, but maybe with different names: like DoneReloadingTime and TimeToReload or something like that.
So, Fire, fire, fire… out of ammo:
now reloading (variable) , and finish time = current time + reload time.
check when the time >= finish time and “reloading” is no more, and you’re set.
There are some other ways you could do this, but that seems pretty simple.
If your situation is more complicated, please explain more