This is for Lesson - “Attacking Force” from Unity 3rd Person Combat Course
Here is a small update to the Press&Release System I mentioned here HERE
If we start attacking and release the targeting button, we are stuck in that state until we press the button again. To fix this, we need a Bool to check if we are still targeting AFTER we finish an attack animation.
I’ve added one in InputReader.cs like this and added it to my OnTarget and OnCancel Methods
public bool IsTargeting { get; private set; }
public void OnTarget(InputAction.CallbackContext context)
{
if (!context.performed) {return;}
TargetEvent?.Invoke();
IsTargeting = true;
}
public void OnCancel(InputAction.CallbackContext context)
{
if (!context.performed) {return;}
CancelEvent?.Invoke();
IsTargeting = false;
}
Then in my PlayerAttackingState.cs - I also check to see if I’m still pressing the targeting button before switching to a new state. Here is how that my Tick method looks
public override void Tick(float deltaTime)
{
Debug.Log(stateMachine.InputReader.IsTargeting);
Move(deltaTime);
FaceTarget();
float normalizedTime = GetNormalizedAtime();
if (normalizedTime >= _previousFrameTime && normalizedTime < 1f)
{
if (normalizedTime >= _attack.ForceTime)
{
TryApplyForce();
}
if (stateMachine.InputReader.IsAttacking)
{
TryComboAttack(normalizedTime);
}
}
else
{
//we check to see if we have a target and if we are still targeting it
if (stateMachine.Targeter.CurrentTarget != null && stateMachine.InputReader.IsTargeting)
{
stateMachine.SwitchState(new PlayerTargetingState(stateMachine));
}
else
{
//if we release the targeting button while we still have a CurrentTarget we remove it before going back to free look
stateMachine.Targeter.Cancel();
stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
}
}
_previousFrameTime = normalizedTime;
}