Skip to content
Permalink
ccdc535e6d
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
50 lines (39 sloc) 1.61 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boss_Run : StateMachineBehaviour
{
//state machine behavior takes from animations states
//speed and range of the circle
public float speed = 2.5f;
public float attackRange = 1f;
//refernces
Transform player;
Rigidbody2D rb;
Boss boss;
// 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)
{
speed = (Random.Range(2f, 4f));
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = animator.GetComponent<Rigidbody2D>();
boss = animator.GetComponent<Boss>();
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
boss.LookAtPlayer();
Vector2 target = new Vector2(player.position.x, rb.position.y);
Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
rb.MovePosition(newPos);
if (Vector2.Distance(player.position, rb.position) <= attackRange)
{
animator.SetTrigger("Attack");
}
}
// 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)
{
animator.ResetTrigger("Attack");
}
}