Hi, i was following the lesson and everything seemed ok but when i pressed play the combo was not working.
I have changed the parameters in the inspector for the attacks and i even tried to mess with the parameters a bit but the results weren’t the expected ones.
I managed only to get a super quick combo, witch was cutting almost all of my animations and ended up waving the sword in the air like the player was drunk.
I run some tests and i found out that my function GetNormalizedTime() always returns the nextinfo.normalizedTime and never the currentinfo.normalizedTime.
I think that the problem may be with the statemachine.animator.IsInTransition(0) because it’s the only parameter that changes in the if statement of the function.
The description of IsInTransition() says that the value is going to be true if there is a transition on the specified animator layer.
Maybe the freelook and targeting blend trees are messing with this value?
I tried to fix it but my solution ended up being to different from the course files and i’m afraid that i won’t be able to continue to follow the course properly having such different code.
I will attach my PlayerAttackingState script down below for you to check it out.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttackingState : PlayerBaseState
{
private float previousFrameTime;
private Attack attack;
public PlayerAttackingState(PlayerStateMachine stateMachine, int AttackIndex) : base(stateMachine)
{
attack = stateMachine.Attacks[AttackIndex];
}
public override void Enter()
{
stateMachine.animator.CrossFadeInFixedTime(attack.AnimationName, attack.TransitionDuration);
}
public override void Tick(float deltaTime)
{
Move(deltaTime);
Facetarget();
float normalizedTime = GetNormalizedTime();
Debug.Log("normalizedTime: " + normalizedTime);
Debug.Log("previousFrameTime: " + previousFrameTime);
if (normalizedTime > previousFrameTime && normalizedTime < 1f)
{
if (stateMachine.inputReader.IsAttacking)
{
TryComboAttack(normalizedTime);
}
}
previousFrameTime = normalizedTime;
}
public override void Exit()
{
}
private void TryComboAttack(float normalizedTime)
{
if(attack.ComboStateIndex == -1f) { Debug.Log("No combo"); return; }
if(normalizedTime < attack.ComboAttackTime) { Debug.Log("Too early"); return; }
Debug.Log("Switching state!");
stateMachine.SwitchState(new PlayerAttackingState(stateMachine,attack.ComboStateIndex));
}
private float GetNormalizedTime()
{
AnimatorStateInfo currentinfo = stateMachine.animator.GetCurrentAnimatorStateInfo(0);
AnimatorStateInfo nextinfo = stateMachine.animator.GetNextAnimatorStateInfo(0);
if(stateMachine.animator.IsInTransition(0) && nextinfo.IsTag("Attack"))
{
Debug.Log("Getting the next normalized time");
return nextinfo.normalizedTime;
}
else if (!stateMachine.animator.IsInTransition(0) && nextinfo.IsTag("Attack"))
{
Debug.Log("Getting the current normalized time");
return currentinfo.normalizedTime;
}
else
{
return 0f;
}
}
}