i need some help with my targeting system.
i want to select the target by touching on it but it wont work correctly.
its probarly because i used the touch position wrong but i don’t know how i should fix that.
static var TARGET : GameObject;
function Update ()
{
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
var midX : int = Screen.width / 2;
var midY : int = Screen.height / 2;
if (touch.position.x > midX-300 touch.position.x < midX+300 touch.position.y > midY-200 touch.position.y < midY+200)
{
print("x = "+ touch.position.x+"y = "+ touch.position.y);
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("enemy");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos)
{
var curDistance = Vector3.Distance(touch.position, go.transform.position);
print(curDistance);
if (curDistance < distance)
{
closest = go;
distance = curDistance;
TARGET = closest;
}
}
}
}
}
}
can someone please help me?