Hello everyone,
I’m making a little game where, in orer to shoot, the user is required to hold his finger onto the screen. I used this code:
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled){
Instantiate (attacco, FirePoint.position, FirePoint.rotation);
AudioSource.PlayClipAtPoint(shot1, transform.position);
}
}
But of course it instantiate a lot of shots, way more than a reasonable quantity. I’d like to know if someone could help me in reducing the fire ratio,using this kind of Input.
Thank you very much in advance.
From your post,My guess is that you want to keep the finger pressed and that should generate shots.Like a machine gun…?If thats the case.use a value to check against the last firing time.
float LastFireTime;
float FireDelay=1.0f; //Use one sec delay between shots
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(Time.time> LastFireTime + FireDelay)
{
Instantiate (attacco, FirePoint.position, FirePoint.rotation);
AudioSource.PlayClipAtPoint(shot1, transform.position);
//Capture the last fire time
LastFireTime =Time.time;
}
}
}
}
Guess this will help u get started…