I’m making a boss and I have some code that’s supposed to make it spin for 5 seconds and then stop spinning. Only problem is the fact that the loop I’m using for this just crashes unity I’ve tried browsing for an answer, I found a thread that says unity will crash if the loop never ends, so I checked and realized my loop was indeed never going to reach the end because the statement that modified the variable to stop the loop was outside the loop so i fixed that but it still crashes.
Code:
public class Spin : StateMachineBehaviour
{
private float damage;
[SerializeField] private float spinDura = 5;
private Breserker breserker;
private Axe_Spin axeSpinHitbox;
// 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)
{
breserker = animator.GetComponent<Breserker>();
axeSpinHitbox = breserker.transform.Find(breserker.axeSpinHitboxName).GetComponent<Axe_Spin>();
axeSpinHitbox.Spin(spinDura);
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
// 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)
{
}
Axe spin hitbox.Spin()
public void Spin(float spinDura)
{
spinning = true;
StartCoroutine(SpinTimer(spinDura));
while (spinning)
{
breserker.transform.Rotate(new(0, spinSpeed, 0));
}
}
public IEnumerator SpinTimer(float spinDura)
{
yield return new WaitForSeconds(spinDura);
spinning = false;
}