Im running into a bit of a problem when trying to destroy one enemy gameobject at a time on touch. Currently in the scene I have several enemy gameobjects moving up the screen. The player taps/touches them to destroy them, but instead of just destroying one enemy gameobject, all of the enemy objects on screen get destroyed all at once when the player touches one. I’ve done some searching but I haven’t come across anything that really helps. Does anybody have any advice?
Here’s the code:
var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
if(hit.collider.gameObject.tag == "Bunny")
By your code all the enemies should die since all of them have the same tag.
you can generate a ray from the mouse and then check which kind of collider it has hit if has hit an enemy object then destroy the collided object.This looks something like this.(there may be some syntax errors)
Ray ray = Camera.mainCamera.ScreenPointToRay( Input.mousePosition );
if( Physics.Raycast( ray, out hitInfo, Mathf.Infinity ) )