Permalink
Cannot retrieve contributors at this time
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?
Platformer/Platformer/Assets/Boss_Run.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (39 sloc)
1.61 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |