Android : Having trouble with firing bullets in unity 3D on Android

on Saturday, August 16, 2014


Here is my code for shooting bullets



void update()
{
if(Input.touches.Length > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
if (shootButton.HitTest(Input.GetTouch(i).position) && Time.time > bulletTime)
{
bulletTime += nextBllet;
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
Vector3 direction;

if (Physics.Raycast(ray, out hit))
{
direction = Vector3.Normalize(hit.point - spawnPoint.position);
}
else
{
direction = spawnPoint.transform.forward;
}

Quaternion rot = Quaternion.FromToRotation(bulletPrefab.transform.forward, direction);

GameObject bull = Instantiate(bulletPrefab, spawnPoint.transform.position, rot) as GameObject;

bull.rigidbody.velocity = direction * 60.0f;
animator.SetTrigger(shootHash);
Destroy(bull.gameObject, 10.0f);
}
}
}
}


It will fire a bullet after 2 seconds when I press the shooting button but if I leave the screen alone for couple of seconds and then start tapping the screen, it will keep on firing bullets and then go back to normal. The longer I leave the screen for, the more bullets it will fire if I just tap the screen. The same code works for keyboard and mouse input but I just cant seem to figure out where the problem is.


Can someone please help me?


0 comments:

Post a Comment