private void selectNewTarget() { // do //{ target = new Vector3(Random.Range(leftPosition.x, rightPosition.x), transform.position.y, Random.Range(bottonPosition.z, topPosition.z)); //} while (Vector3.Distance(target, transform.position) > movementRange); movementController.moveTo(target); }
// Use this for initialization void Start() { topPosition = GameObject.Find("TopLimit").transform.position; bottonPosition = GameObject.Find("BottomLimit").transform.position; leftPosition = GameObject.Find("LeftLimit").transform.position; rightPosition = GameObject.Find("RightLimit").transform.position; movementController = GetComponent<EnemyMovementController>(); target = new Vector3(Random.Range(leftPosition.x, rightPosition.x), transform.position.y, Random.Range(bottonPosition.z, topPosition.z)); movementController.moveTo(target); InvokeRepeating("selectNewTarget", 15, 10); }
// Use this for initialization void Start() { topPosition = GameObject.Find("TopLimit").transform.position; bottonPosition = GameObject.Find("BottomLimit").transform.position; leftPosition = GameObject.Find("LeftLimit").transform.position; rightPosition = GameObject.Find("RightLimit").transform.position; movementController = GetComponent <EnemyMovementController>(); target = new Vector3(Random.Range(leftPosition.x, rightPosition.x), transform.position.y, Random.Range(bottonPosition.z, topPosition.z)); movementController.moveTo(target); InvokeRepeating("selectNewTarget", 15, 10); }
void Update() { float distance = Vector3.Distance(transform.position, target.transform.position); Ray ray = new Ray(transform.position, (target.transform.position - transform.position).normalized); RaycastHit hit; if (Physics.Raycast(ray, out hit, distance + 10)) { if (Vector3.Distance(hit.point, transform.position) <= attackComponent.getAttackRange() && (hit.collider.gameObject.layer == 10 || hit.collider.gameObject.layer == 15)) { movementController.stop(); attackComponent.AttackTo(target); } else { movementController.moveTo(target.transform.position); } } else { movementController.moveTo(target.transform.position); } }