For the last week ive been trying to make a script where i use the input.touch, to kill the object its attached to, when u press on the smartphone screen.
The difference from mousebuttondown is, that i want to have the opportunity to tap and kill more than one enemy at a time.
Ive read the physics.raycast2D, multitouch manuel, and seen a lot of answers and youtube video, but nothing has worked…
right now im working with this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class testInput : MonoBehaviour {
private RaycastHit2D hit;
public int health = 3;
private int speed = 2;
void Update ()
{
//transform.Translate(0,-speed * Time.deltaTime,0);
Touch myTouch = Input.GetTouch(0);
Touch[] myTouches = Input.touches;
for(int i = 0; i < Input.touchCount; i++)
{
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.touches*.position), Vector2.zero);*
if (Input.touches*.phase == TouchPhase.Began) {*
if(hit.collider.tag == “enemy”) {
health = health-1;
}
}
if(health <= 0) {
die();
}
}
}
void die () {
Destroy(hit.transform.gameObject);
}
}
The problem is, that it affects all the objects that the script is attached to, so every object gets -1 in health… I know its probably something with the tag, but i can’t really find any other solution. Please help a man in pain.
Pls someone answer this. Im dying.
– NinjaRubberBand