void TryHarvestFruit() { Collider2D hit = Physics2D.OverlapCircle(transform.position, harvestRadius, bushesMask); if (hit != null) { BushFruits bush = hit.GetComponent <BushFruits>(); if (bush.HasFruits()) { audioSource.Play(); playerMovement.HarvestStopMovement(harvestTime); backpack.AddFruits(bush.HarvestFruit()); } } }
void Update() { if (isEater) { //Main eater wolf logic: if (target != null && target.enabled == true && target.HasFruits() && !killingBush) { //If not close enough to bush, continue walking towards it, else stop and eat the bush. if (Vector2.Distance(transform.position, target.transform.position) > 0.5f) { float step = moveSpeed * Time.deltaTime; transform.position = Vector2.MoveTowards(transform.position, target.transform.position, step); isMoving = true; } else { isMoving = false; target.HarvestFruit(); eatTimer = Time.time + eatTime; killingBush = true; } } else if (killingBush) { //Wait for eatTimer, then search next closest bush. if (Time.time > eatTimer) { target.EatBush(); killingBush = false; SearchForTarget(); } } else //In some cases, a wolf can target the same bush that other wolf targeted, so when the other wolf eats it, its no longer valid for this wolf to continue targeting it. { SearchForTarget(); } //target based sprite flipping. if (target.transform.position.x < transform.position.x) { left = true; } else { left = false; } //Error handling if (target == null) { SearchForTarget(); } } else { //Main attacker wolf logic: if (Vector2.Distance(transform.position, artifact.transform.position) > 1.5f) { //if not close enough to artifact, continue moving float step = moveSpeed * Time.deltaTime; transform.position = Vector2.MoveTowards(transform.position, artifact.transform.position, step); isMoving = true; } else if (!attacking) { //Start attacking when close enough attacking = true; attackTimer = Time.time + attackTime; isMoving = false; } if (attacking) { if (Time.time > attackTimer) { Attack(); attackTimer = Time.time + attackTime; } } //Artifact based sprite flipping if (artifact.transform.position.x < transform.position.x) { left = true; } else { left = false; } } }