UPDATED - Target State - Press & Release same Button

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;
    }

@Nina can you please add the correct tags to this post - its for 16_MC_UTP “Attacking Force” from Unity 3rd Person Combat Course

@mihaivladan, done. :slight_smile:

If you dont use the system you mentioned and just followed the videos do you have to hold to attack or is the first attack supposed to play even if you release?

The changes I suggest don’t impact the attack animation or how it works. They only affect how you enter and exit the target state.

How long the attack animation plays is controlled by the settings in the attack list ( where we set the damage/force/etc.). The combo attack time and transition duration are the values that control the animation duration.

Privacy & Terms