Question by
Nosmo · Aug 21, 2019 at 05:00 PM ·
shoottouchscreeninput.touchtouchcountgettouch
I can't fire using the fire button with Input.GetTouch
The aim of the mobile game is to fire prefab bullets at an oncoming enemy which works. But i want to change the rate of fire in different levels to make it more challenging.
At the moment it fires whenever I touch anywhere on the screen, I'm trying to fire just when i press the fire button
Does anyone have any suggestions?
public class Weapon : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public float fireRate = 0.25f; //addition
private float nextfire; //addition
private AudioSource audioSource;
public void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void Update()
{
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began) //&& Time.time > nextfire) //addition
//if (Input.GetButtonDown("Fire1") && Time.time > nextfire)
{
Shoot();
}
}
}
public void Shoot()
{
nextfire = Time.time + fireRate;
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
// Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
audioSource.Play();
Debug.Log("Bang: weapon script");
}
}
Comment
What if you get rid of the Update
and specify the following function in the onClick
event of the button?
public void Shoot()
{
if( Time.time < nextfire )
return ;
nextfire = Time.time + fireRate;
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
// Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
audioSource.Play();
Debug.Log("Bang: weapon script");
}