void TargetNearest() { List <GameObject> validTargets = locator.GetTargets(); //List of targets from the enemy locator GameObject curTarget = null; float closestDist = 0.0f; for (int i = 0; i < validTargets.Count; ++i) { if (validTargets[i] == null || validTargets[i].tag != "Enemy") //If enemy is destroyed, remove it from list { validTargets.RemoveAt(i); --i; continue; } float dist = Vector3.Distance(transform.position, validTargets[i].transform.position); if (!curTarget || dist < closestDist) //If the distance between the turret and the target is less than the closest distance { curTarget = validTargets[i]; closestDist = dist; //Set the target to that enemy and reduce the closest distance } } if (this.transform.parent.name == "RocketTower(Clone)") { // Debug.Log("AssignedTarget"); rocketTower.SetTarget(curTarget); } else { movement.SetTarget(curTarget); //Return the closest target to the turret movement script } }
void Update() { if (Input.GetMouseButton(0)) //If click { if (GameObject.Find(mouseTarget)) { DestroyImmediate(GameObject.Find(mouseTarget)); } Ray cameraRay = cam.ScreenPointToRay(Input.mousePosition); //Ray from camera Plane groundPlane = new Plane(Vector3.up, new Vector3(0, 0, 0)); //Plane used for getting position of finger float rayLength; if (groundPlane.Raycast(cameraRay, out rayLength)) //If the ray from the camera has hit the plane { Debug.DrawLine(cameraRay.origin, cameraRay.GetPoint(rayLength), Color.red); GameObject mousePoint = new GameObject(mouseTarget); mousePoint.transform.position = cameraRay.GetPoint(rayLength); //Set where the point of collision to where ray from the camera hits the plane. movement.SetTarget(GameObject.Find(mouseTarget)); //Set target for manual turrets to look at. } } }