Search Unity

Question Can I prevent 2 positive inputs of an axis producing an amplified result?

Discussion in 'Input System' started by JoeStinky, Jan 20, 2023.

  1. JoeStinky

    JoeStinky

    Joined:
    Jun 22, 2021
    Posts:
    4
    To explain a whole lot better:
    I have set the jump axis in my input manager to the 'w' key and 'up' arrow key and if I press both buttons at the same time, I jump higher than intended.
    I've taken this code from Brackeys and was wondering if there is a way to prevent this.

    Here is the PlayerMovement script where it mainly takes the input of the player(I've highlighted the appropriate code with brackets but left everything else in just incase)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController2D controller;
    8.  
    9.     public float runSpeed = 30f;
    10.     float horizontalMove = 0f;
    11.     [bool jump = false;]
    12.  
    13.     public Animator animator;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.  
    22.     void Update()
    23.     {
    24.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    25.  
    26.         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    27.  
    28.         [if (Input.GetButtonDown("Jump"))
    29.         {
    30.             jump = true;
    31.         }]
    32.     }
    33.     private void FixedUpdate()
    34.     {
    35.         [controller.Move(horizontalMove*Time.fixedDeltaTime, false, jump);
    36.         jump = false;]
    37.     }
    38. }
    39.  
    And here is the CharacterController script where it actually applies the player's input and moves the character based off of the inputs it's given(Here I only included code that affects the jumping in any way as there is too much excess information that has nothing to do with the jump)
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. public class CharacterController2D : MonoBehaviour
    4. {
    5.     [SerializeField] private float m_JumpForce = 400f;                      
    6.     [SerializeField] private LayerMask m_WhatIsGround;
    7.     [SerializeField] private Transform m_GroundCheck;
    8.  
    9.     const float k_GroundedRadius = .2f;
    10.     private bool m_Grounded;
    11.  
    12.     private void Awake()
    13.     {
    14.         m_Rigidbody2D = GetComponent<Rigidbody2D>();
    15.  
    16.         if (OnLandEvent == null)
    17.             OnLandEvent = new UnityEvent();
    18.  
    19.         if (OnCrouchEvent == null)
    20.             OnCrouchEvent = new BoolEvent();
    21.     }
    22.  
    23.     private void FixedUpdate()
    24.     {
    25.         bool wasGrounded = m_Grounded;
    26.         m_Grounded = false;
    27.  
    28.         // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    29.         // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    30.         Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    31.         for (int i = 0; i < colliders.Length; i++)
    32.         {
    33.             if (colliders[i].gameObject != gameObject)
    34.             {
    35.                 m_Grounded = true;
    36.                 if (!wasGrounded)
    37.                     OnLandEvent.Invoke();
    38.             }
    39.         }
    40.     }
    41.  
    42.  
    43.     public void Move(float move, bool crouch, bool jump)
    44.     {
    45.         // If the player should jump...
    46.         if (m_Grounded && jump)
    47.         {
    48.             // Add a vertical force to the player.
    49.             m_Grounded = false;
    50.             m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    51.         }
    52.     }
    53.  
    54.  
    55. }
    I assume that when two buttons of the same axis are pressed at the same time that it would makes sense for them to both affect whatever it is they're meant to so I didn't prefix this as a bug per se, but is there a way to prevent this from being the case?
     
  2. Parlay-Creative

    Parlay-Creative

    Joined:
    Sep 14, 2013
    Posts:
    7
    Since you're setting
    jump
    as a boolean in your PlayerMovement and not reading the value, the issue isn't related to two input buttons being pressed at once. FixedUpdate and Update are not called with the same frequency. Update is called exactly once per frame, FixedUpdate is called at a fixed time step which may be more than once a frame. Try reading your input in FixedUpdate and see if the at corrects your issue.