public IEnumerator MoveAround() { while (idle) { float randX = Random.Range(-8f, 8f); float randY = Random.Range(-4f, 4f); Vector2 newPos = new Vector2(randX, randY); if (randX < transform.position.x) { transform.localScale = new Vector3(-4f, 4f, 1f); } else { transform.localScale = new Vector3(4f, 4f, 1f); } StartCoroutine(MoveTools.SmoothMovement(gameObject, newPos, 0f, 5f)); GetComponent <Rigidbody2D>().velocity = Vector3.zero; yield return(new WaitForSeconds(1f)); } }
IEnumerator Shooting() { while (targetBird != null) { if (Vector3.Distance(targetBird.transform.position, transform.position) > 2f) { StartCoroutine(MoveTools.SmoothMovement(gameObject, targetBird.transform.position, 2f, 3f)); } var bullet = Instantiate(spit, transform.GetChild(0).position, Quaternion.identity); bullet.GetComponent <DuckSpit>().target = targetBird; yield return(new WaitForSeconds(1f)); } }
void Routine() { birds = FindObjectsOfType <BirdIdle>(); float x = Mathf.Infinity; foreach (BirdIdle bird in birds) { if (Vector2.Distance(bird.transform.position, transform.position) < x) { targetBird = bird.gameObject; } } StartCoroutine(MoveTools.SmoothMovement(gameObject, targetBird.transform.position, 2f, 3f)); StartCoroutine(Shooting()); }