0
\$\begingroup\$

I am making a 2.5D runner game and I want to switch my input from the legacy Input Manager to Unity's new Input System package.

I am facing a problem when I'm trying to implement the double jump when the player presses space twice. With the legacy Input Manager, there was bool CanDoubleJump which is true when the player is grounded, for double jump I was checking if CanDoubleJump is true and player pressed jump. It was working perfectly fine. Here's is the code.

using UnityEngine;

namespace EndlessRunning
{
    [CreateAssetMenu(fileName = "New Object", menuName = "ScriptableObject/Ability/DoubleJump")]
    public class DoubleJump : ScriptableObjectData
    {
        [SerializeField]
        private bool CanDoubleJump = true;

        public override void OnEnter(PlayerStateBase playerStateBase, Animator animator, AnimatorStateInfo stateInfo)
        {

        }
        public override void OnUpdate(PlayerStateBase playerStateBase, Animator animator, AnimatorStateInfo stateInfo)
        {

            //if (playerStateBase.characterControl.DoubleJump)
            //{
            //    animator.SetBool(HashManger.Instance.DicMainParameters[AnimatorParameters.DoubleJump], true);
            //    playerStateBase.characterControl.CanDoubleJump = false;
            //}

            if (playerStateBase.characterControl.Grounded)
            {
                CanDoubleJump = true;
            }
            else
            {
                if (CanDoubleJump && playerStateBase.characterControl.Jump)
                {
                    animator.SetBool(HashManger.Instance.DicMainParameters[AnimatorParameters.DoubleJump], true);
                    CanDoubleJump = false;
                }
            }
        }

        public override void OnExit(PlayerStateBase playerStateBase, Animator animator, AnimatorStateInfo stateInfo)
        {
        }
    }
}
``|

I tried to do the same thing with the new Input System, but it is not working: the character goes into double jump even if space is pressed only once. I am using Unity events for calling the function, and `CandoubleJump` is defined in character control, working the same way. 

**Code** 

```cs
using UnityEngine;
using UnityEngine.InputSystem;

namespace EndlessRunning
{
    public class PlayerInputController : MonoBehaviour
    {
        [SerializeField]
        private CharacterControl control;
        public PlayerInput inputComp;

        private void Awake()
        {

            inputComp = GetComponent<PlayerInput>();
            control = GetComponent<CharacterControl>();
        }

        private void Update()
        {
            //RunModInput() Legacy;
        }

        //input system new
        public void JumpMethod(InputAction.CallbackContext context)
        {
            if (context.performed)
            {
                if (!control.StartRun)
                {
                    control.StartRun = true;
                }
                else
                {
                    control.Jump = true;
                }

                if (control.CanDoubleJump && control.Jump)
                {
                    control.DoubleJump = true;
                    control.CanDoubleJump = false;
                }
            }
            else
            {
                control.Jump = false;
                control.DoubleJump = false;
            }
        }


        #region Run Mod Input legacy
        //private void RunModInput()
        //{
        //    if (InputManger.Instance.StartRun)
        //    {
        //        control.StartRun = true;
        //    }
        //    else
        //    {
        //        control.StartRun = false;
        //    }
        //    if (InputManger.Instance.Jump)
        //    {
        //        control.Jump = true;
        //    }
        //    else
        //    {
        //        control.Jump = false;
        //    }
        //    if (InputManger.Instance.Slide)
        //    {
        //        control.Slide = true;
        //    }
        //    else
        //    {
        //        control.Slide = false;
        //    }
        //    if (InputManger.Instance.Dash)
        //    {
        //        control.Dash = true;
        //    }
        //    else
        //    {
        //        control.Dash = false;
        //    }
        //}
        #endregion
    }
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

If control.StartRun == false and control.CanDoubleJump == true you are double-jumping right after jumping.

//...
            else
            {
                control.Jump = true;
            }

            if (control.CanDoubleJump && control.Jump)
            {
                control.DoubleJump = true;
                control.CanDoubleJump = false;
            }
//...
\$\endgroup\$
2
  • 2
    \$\begingroup\$ I think you found the problem. But can you also show the question author a solution for it? \$\endgroup\$
    – Philipp
    Commented Aug 29, 2023 at 11:13
  • \$\begingroup\$ nah i also had to check ground for double jump player must be not grounded \$\endgroup\$ Commented Aug 29, 2023 at 17:47
0
\$\begingroup\$

figured out the solution: updated code i also had to check that the player must not be grounded for the double jump to work

  public void JumpInput(InputAction.CallbackContext context)
    {
        if (context.performed)
        {
            if (!control.StartRun)
            {
                control.StartRun = true;
            }
            else
            {
                control.Jump = true;

                if (!control.Grounded && control.CanDoubleJump && control.Jump)
                {
                    control.DoubleJump = true;
                    control.CanDoubleJump = false;
                }
            }

        }
        else
        {
            control.Jump = false;
            control.DoubleJump = false;
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .