Search Unity

Bug Multiple Interactions for the same binding help

Discussion in 'Input System' started by SundersoftCEO, Apr 30, 2023.

  1. SundersoftCEO

    SundersoftCEO

    Joined:
    Apr 9, 2023
    Posts:
    1
    Hi there! I'm making something of a flappy bird clone with a twist. Thus far I've made the base clone game where jump is bound to a tap of the spacebar. My problem is now I'm attempting to introduce a slow-motion mechanic when the spacebar is held. This doesn't seem like it would be particularly difficult, and it wasn't, I can get both jump and slow motion to work, but I can't get them both to work at the same time. I'm also having an issue that I'm not understanding why I can't disable the controls in a game over state.

    To begin I made this game on the legacy input system, but was having difficulty making that work due to GetKey and GetKeyDown both returning true on the first frame (slow-mo and jump are not supposed to trigger together, a quick tap is supposed to trigger jump, and a hold is supposed to trigger slow motion), so I thought the new system would fix this issue, and it did.

    My current problem is that depending on if I place my time normalization method (OnHoldCancel()) in the editor's Event Behavior everything changes. If OnHoldCancel() is in the editor's player event behaviors I can play normally, but time doesn't slow time upon a hold, and if I hold while playin, it becomes something like a 1.5 jump or double jump for some reason. If I don't include OnHoldCancel() in the event behaviors it jumps and slows down time immediately on a tap; furthermore I have no way of turning off the slowdown time or separating the two commands which makes no sense because I made sure their have separate press points, hold points, and cancel points. I thought the subscribe and unsubscribe cases in the Awake() method would fix it, but it doesn't seem to be doing much. I feel like I'm just missing something simple. I also am having trouble understanding why birdControls.Disable() isn't working, as I thought having it in update referencing my birdIsAlive bool would make it check for the bool being false every frame, and upon it becoming false, theoretically should activate birdControls.Disable()...

    Following is all the relevant code for the project...

    Input Manager:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class NewInputManagerScript : MonoBehaviour{
    5.     //Variables
    6.         //General Variables
    7.             public Rigidbody2D myRigidbody;        
    8.             public BirdControls birdControls;
    9.             public BirdScript birdScriptInput;
    10.         //Flap Stuff
    11.             [SerializeField] float flapStrength;
    12.         //Ninja Stuff
    13.             public timeManagerScript timeManager;
    14.  
    15.  
    16.     //Initialize+Update
    17.     void Awake(){
    18.             birdControls.Player.Jump.performed += J => OnTap();
    19.             birdControls.Player.BladeMode.started += BM => OnHold();
    20.             birdControls.Player.BladeMode.performed -= BM => OnHold();
    21.             birdControls.Player.BladeMode.performed += CBM => OnHoldCanceled();
    22.         }
    23.     void Update(){
    24.             if (!birdScriptInput.birdIsAlive)
    25.             {
    26.                 birdControls.Disable();
    27.             }
    28.         }
    29.  
    30.  
    31.     //Enable+Disable
    32.     void OnEnable() {
    33.             birdControls.Enable();
    34.         }
    35.         /*void OnDisable() {
    36.            
    37.         }*/
    38.  
    39.  
    40.     //Tap Stuff
    41.         public void OnTap(){
    42.             myRigidbody.velocity = Vector2.up * flapStrength;
    43.         }
    44.  
    45.  
    46.     //Hold Stuff
    47.         public void OnHold(){
    48.             timeManager.DoSlowMotion();
    49.         }
    50.  
    51.         public void OnHoldCanceled(){
    52.             timeManager.CancelSlowMotion();
    53.         }
    54.  
    55. }
    Time Manager:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class timeManagerScript : MonoBehaviour
    4. {
    5.     [SerializeField] float slowdownFactor = 0.5f;
    6.     public BirdScript birdScriptRef;
    7.     //bool isNormalTime = true;
    8.  
    9.     /*void Update()
    10.     {
    11.         isNormalTime = birdScriptRef.normalTime;
    12.     }*/
    13.  
    14.     public void DoSlowMotion()
    15.     {
    16.         Time.timeScale = slowdownFactor;
    17.         Time.fixedDeltaTime = Time.timeScale * .02f;
    18.     }
    19.  
    20.     public void CancelSlowMotion()
    21.     {
    22.         Time.timeScale = 1f;
    23.         Time.fixedDeltaTime = Time.unscaledDeltaTime;
    24.     }
    25. }
    Player Logic:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BirdScript : MonoBehaviour
    4. {
    5.     //Variables
    6.     public LogicScript logic;
    7.     public timeManagerScript timeManager;
    8.     public bool birdIsAlive = true;
    9.     void Start()
    10.     {
    11.         logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScript>();
    12.     }
    13.     void Update()
    14.     {
    15.         OutOfBounds();
    16.     }
    17.     private void OutOfBounds()
    18.     {
    19.         if (transform.position.y > 14 || transform.position.y < -14)
    20.         {
    21.             logic.gameOver();
    22.             birdIsAlive = false;
    23.         }
    24.     }
    25.     void OnCollisionEnter2D(Collision2D collision)
    26.     {
    27.         logic.gameOver();
    28.         birdIsAlive = false;
    29.     }
    30. }
    The following are screen caps of my action map and a view of my inspector with the relevant player game object selected.


    upload_2023-4-30_16-42-9.png


    upload_2023-4-30_16-42-30.png