Shoot until i release the button, but with a fire ratio
I'm creating a top down shooter for mobile, with only a single stick controller that move my player and changes his direction, i have also a "Fire" button that should make the rifle shoot until it is pressed but with a specific fire ratio. I tried Coroutine, and it shoot only when i press and when i release (i even tried to change the event in event trigger between pointer click and pointer down), then i tried this following code:`public void OnClickFire() {
if(Time.time > NextFire)
{
NextFire = Time.time + FireRate;
Instantiate(bullet, transform.position, transform.rotation);
}
}`
but again it won't work. I just want to press the button and shoot, limited by the only Fire Ratio, not by the click of the button! Thank you!
Answer by tormentoarmagedoom · Sep 30, 2018 at 03:14 PM
Good day.
A good simple solution, should be create a corrutine that starts with GetKeydown.
At the end of the corrutine, detect GetKey. If the key is still pressed, call the corutine again.
Bye!
Answer by Hellium · Sep 30, 2018 at 05:41 PM
public Weapon : MonoBehaviour
{
private bool firing = false ;
public GameObject bullet ;
void Update()
{
if( firing && Time.time > NextFire )
{
NextFire = Time.time + FireRate;
Instantiate(bullet, transform.position, transform.rotation);
}
}
public void StartFiring()
{
firing = true ;
}
public void StopFiring()
{
firing = false;
}
}
Then, call the StartFiring
and StopFiring
functions using an EventTrigger
component you attach to your button with the PointerDown
and PointerUp
events specified.