Hey,guys I have a script that when the player touches the screen it fires a ray cast and if it hits an enemy it destroys it. However when the player holds their finger on the screen the ray cast fires continuously instead of just once how do I make it so the ray cast only fires once each the player presses the screen thanks
my code
if (Input.touchCount > 0){
Vector2 dir = Vector2.zero;
foreach (Touch touch in Input.touches)
{
Vector3 touchworldpos = Camera.main.ScreenToWorldPoint(touch.position);
Vector2 touchpos2d = new Vector2(touch.position.x, touch.position.y);
Debug.Log(touchpos2d);
RaycastHit2D hit = Physics2D.Raycast(touchworldpos, dir);
Debug.DrawRay(touchworldpos, dir, Color.blue);
if (hit != null && hit.collider != null)
{
if (hit.collider.tag == " germ one")
{
if (hit.collider.name == "germ purple(Clone)")
Instantiate(spaltObject[2], hit.point, Quaternion.identity);
if (hit.collider.name == "Germ blue(Clone)")
Instantiate(spaltObject[1], hit.point, Quaternion.identity);
if (hit.collider.name == "Germ red(Clone)")
Instantiate(spaltObject[3], hit.point, Quaternion.identity);
if (hit.collider.name == "green germ(Clone)")
Instantiate(spaltObject[5], hit.point, Quaternion.identity);
die.Play();
Destroy(hit.collider.gameObject);
Addscore.AddScore();
player.transform.localScale += new Vector3(0.5f, 0.5f);
if (Addscore.score % 10 == 0)
player.transform.localScale -= new Vector3(4.5f, 4.4f);
if(Addscore.score %50 ==0)
Pluslevel();
}
else if(hit.collider.tag =="heart")
if(playerHealth.health<3){
heartsound.PlayOneShot(pickUpsound);
playerHealth.health+=1;
Destroy (hit.collider.gameObject);
}
if(hit.collider.tag == "germ black"){
if (hit.collider.name == "black germ(Clone)")
blackgermhealth--;
if(blackgermhealth<=0)
die.Play();
Instantiate(spaltObject[4], hit.point, Quaternion.identity);
Destroy(hit.collider.gameObject);
Addscore.AddScore();
player.transform.localScale += new Vector3(0.5f, 0.5f);
}
}
}
}
}
All this is in void Update thanks