void Update() { bool destroy = false; for (int t = 0; t < fishGenerator.fishes.Count; t++) { if (fishGenerator.sqrDistanceFromPointToPoint(fishGenerator.fishes[t].transform.position, transform.position) < pickUpRadius * pickUpRadius) { destroy = true; break; } } if (destroy) { for (int i = 0; i < fishCount; i++) { GameObject newFish = Instantiate(fishGenerator.fish, transform.position + new Vector3(Random.Range(-0.5f, 0.5f), Random.Range(-0.5f, 0.5f), Random.Range(-0.5f, 0.5f)) , Quaternion.identity); newFish.GetComponent <FishMind>().SetFishGenerator(fishGenerator); newFish.GetComponent <SpriteRenderer>().sortingOrder = Random.Range(10, 39); fishGenerator.fishes.Add(newFish.GetComponent <FishMind>()); } Destroy(gameObject); } transform.position += Vector3.right * speed * Time.deltaTime; if (((float)Screen.width / Screen.height) * fishGenerator.cameraSize + 10 < transform.position.x) { Destroy(gameObject); } }
void FixedUpdate() { if (!getTarget) { Vector3 target = fishGenerator.FindCentreOfMassOfFishes(); Vector3 direction = (target - transform.position).normalized; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, transform.forward); targetPosition += direction * speed * Time.fixedDeltaTime; Vector3 vectorToMove = Vector3.Lerp(transform.position, targetPosition, smoothing); rigidbody.MovePosition(vectorToMove); if (fishGenerator.sqrDistanceFromPointToPoint(transform.position, target) < getTargetRadius * getTargetRadius) { getTarget = true; } } else { Vector3 targetVector = transform.position + transform.right * speed * Time.fixedDeltaTime; Debug.Log(transform.right); rigidbody.MovePosition(targetVector); } if (fishGenerator.sqrDistanceFromPointToPoint(transform.position, Vector3.zero) > deathRadius) { Destroy(gameObject); } for (int t = 0; t < fishGenerator.fishes.Count; t++) { if (fishGenerator.sqrDistanceFromPointToPoint(fishGenerator.fishes[t].transform.position, transform.position) < pickUpRadius * pickUpRadius) { FishMind fishMind = fishGenerator.fishes[t]; fishGenerator.fishes.Remove(fishMind); Destroy(fishMind.gameObject); } } }