Double Jumping

If I spam the space bar I can fly to outer space. Is this going to come up later or did I miss something? I seems like I probably need to do an if statement somewhere for IsOnGround but I am not confident about where to do that. Thanks ahead of time.

No one likes spam :wink:

Yeah, you’d have to do something like that.

If you never subscribe to the Jump event in PlayerJumpingState or the PlayerFallingState, the player shouldn’t be able to double jump.

Seems like its only doing it while in the targeting state. Maybe that was an optional thing and I just did it wrong. In free look state it does just what it is supposed to. From the targeting state it will jump forever. let me paste in that script:

public class PlayerTargetingState : PlayerBaseState


{
    private float remainingDodgeTime;
    private Vector2 dodgingDirectionInput;
    private readonly int TargetingBlendTreeHash = Animator.StringToHash("TargetingBlendTree");
    private readonly int TargetingForwardSpeedHash = Animator.StringToHash("TargetingForwardSpeed");
    private readonly int TargetingRightSpeedHash = Animator.StringToHash("TargetingRightSpeed");
    private const float CrossFadeDuration = .1f;

    public PlayerTargetingState(PlayerStateMachine stateMachine) : base(stateMachine)
    {

    }

  
    public override void Enter()
    {
        stateMachine.InputReader.CancelEvent += OnCancel;
        stateMachine.Animator.CrossFadeInFixedTime(TargetingBlendTreeHash, CrossFadeDuration);
        stateMachine.InputReader.DodgeEvent += OnDodge;
        stateMachine.InputReader.JumpEvent += OnJump;
    }

    public override void Tick(float deltaTime)
    {
        if(stateMachine.InputReader.IsAttacking)
        {
            stateMachine.SwitchState(new PlayerAttackingState(stateMachine,0));
            return;
        }
        if (stateMachine.InputReader.IsBlocking)
        {
            stateMachine.SwitchState(new PlayerBlockingState(stateMachine));
            return;
        }
        if (stateMachine.InputReader.IsJumping)
        {
            stateMachine.SwitchState(new PlayerJumpingState(stateMachine));
            return;
        }
        if (stateMachine.Targeter.currentTarget == null)
        {
            stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
            return;

        }

       
        Vector3 movement = CalculateMovement(deltaTime);
        Move(movement * stateMachine.TargetingMovementSpeed, deltaTime);
        UpdateAnimator(deltaTime);
        FaceTarget();
        //Debug.Log(stateMachine.Targeter.currentTarget.name);
    }

    public override void Exit()
    {
        stateMachine.InputReader.CancelEvent -= OnCancel;
        stateMachine.InputReader.DodgeEvent -= OnDodge;
        stateMachine.InputReader.CancelEvent -= OnJump;
    }

    private void OnCancel()
    {
        stateMachine.Targeter.Cancel();
        stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
    }
    private void OnDodge()
    {
        if(Time.time - stateMachine.previousDodgeTime < stateMachine.DodgeCooldown) { return; }
        stateMachine.SetDodgeTime(Time.time);
        dodgingDirectionInput = stateMachine.InputReader.MovementValue;
        remainingDodgeTime = stateMachine.DodgeDuration;
    }
    private void OnJump()
    {
        stateMachine.SwitchState( new PlayerJumpingState(stateMachine));
    }

    private Vector3 CalculateMovement(float deltaTime)
    {
        Vector3 movement = new Vector3();
        if(remainingDodgeTime > 0f)
        {
            movement += stateMachine.transform.right * dodgingDirectionInput.x * (stateMachine.DodgeLength / stateMachine.DodgeDuration);
            movement += stateMachine.transform.forward * dodgingDirectionInput.y * (stateMachine.DodgeLength / stateMachine.DodgeDuration);

            remainingDodgeTime = Mathf.Max(remainingDodgeTime - deltaTime, 0f);

          
        }
        else
        {
            movement += stateMachine.transform.right * stateMachine.InputReader.MovementValue.x;
            movement += stateMachine.transform.forward * stateMachine.InputReader.MovementValue.y;
        }
       
        return movement; 
    }
    private void UpdateAnimator(float deltaTime)
    {
        if (stateMachine.InputReader.MovementValue.y == 0)
        {
            stateMachine.Animator.SetFloat(TargetingForwardSpeedHash, 0, .1f, deltaTime);
        }
        else
        {
            float value = stateMachine.InputReader.MovementValue.y > 0 ? 1f : -1f;
            stateMachine.Animator.SetFloat(TargetingForwardSpeedHash, value, .1f, deltaTime);
        }
        if (stateMachine.InputReader.MovementValue.x == 0)
        {
            stateMachine.Animator.SetFloat(TargetingRightSpeedHash, 0, .1f, deltaTime);
        }
        else
        {
            float value = stateMachine.InputReader.MovementValue.x > 0 ? 1f : -1f;
            stateMachine.Animator.SetFloat(TargetingRightSpeedHash, value, .1f, deltaTime);
        }

    }

    
}

crap i think i see it!

stupid typo.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

For those that might be wondering…

1 Like

Privacy & Terms