using System.Collections; using System.Collections.Generic; using UnityEngine; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class Boss_Run : StateMachineBehaviour { public float speed = 2.5f; // Speed at which the boss moves public float attackRange = 3f; // Range within which the boss will attack Transform player; // Reference to the player’s transform Rigidbody2D rb; // Reference to the boss's Rigidbody2D component Boss boss; // Reference to the Boss script // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { // Find and store references to player, Rigidbody2D, and Boss script components player = GameObject.FindGameObjectWithTag("Player").transform; rb = animator.GetComponent(); boss = animator.GetComponent(); } // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { // Make the boss look at the player boss.LookAtPlayer(); // Calculate the target position (boss should move horizontally towards the player) Vector2 target = new Vector2(player.position.x, rb.position.y); // Move the boss towards the target position Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime); rb.MovePosition(newPos); // If the distance between the boss and the player is within attack range, trigger an attack if (Vector2.Distance(player.position, rb.position) <= attackRange) { animator.SetTrigger("isAttacking"); } } // OnStateExit is called when a transition ends and the state machine finishes evaluating this state override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { // Reset the attack trigger when exiting this state animator.ResetTrigger("isAttacking"); } }