Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug Key pressed immediately triggers canceled phase, even when held down

Discussion in 'Input System' started by TheGoldenProof, Apr 7, 2021.

  1. TheGoldenProof

    TheGoldenProof

    Joined:
    Oct 7, 2020
    Posts:
    2
    I'm hesitant to mark this as a bug because I might just be doing something wrong, but I can't think of any reason it should logically be like this.

    So here's what's happening: I have a very simple 2D world, and all I'm trying to do is move a sprite when movement inputs are down. The sprite does move, so there's no problems there.
    Where there is a problem, however, is that when I press a movement key on my keyboard, it only occasionally moves one single frame. I couldn't figure out why this was happening until I started logging the action phase. The phase goes from started to performed to canceled in a single frame, regardless of whether I hold the button down. On the other hand, when I use a controller, it works just fine.
    I haven't modified the move action or anything under it, so I can't think of any reason why it wouldn't work.

    Some more specific information about the setup:
    The thing I'm trying to move is actually an image in a canvas. On the canvas, I have a Player Input component which is set to Invoke Unity Events. I have a script on the canvas which has a function to be called when it receives the move event, and it moves the image. The Input Action Asset as well as the Input Settings I'm using are the auto-generated ones.
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    I created a project based on your description: UI Canvas with an Image on it. Added a Player Input to the Canvas and clicked the button on the Event System object that replaces the old input component with the new Input System UI Input Module (I'm assuming you did that too, or else you'd be getting endless error messages when you run your game). Then added this script to the Canvas:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class EventReporter : MonoBehaviour
    5. {
    6.     public void Report(InputAction.CallbackContext c)
    7.     {
    8.         Debug.Log($"Callback {c.phase}");
    9.     }
    10. }
    11.  
    And I added the Report method to the Move event:
    upload_2021-4-9_13-42-25.png

    When I run and press "A" without releasing it, I get this output:
    upload_2021-4-9_13-43-33.png

    When I then release the "A" key, it adds another line:
    upload_2021-4-9_13-44-2.png

    What's your code look like?
     
  3. TheGoldenProof

    TheGoldenProof

    Joined:
    Oct 7, 2020
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class GameController : MonoBehaviour {
    7.     public Player player;
    8.  
    9.     void Start() {
    10.  
    11.     }
    12.  
    13.     void Update() {
    14.  
    15.     }
    16.  
    17.     public void OnMove(InputAction.CallbackContext e) {
    18.         Vector2 input = e.ReadValue<Vector2>();
    19.         Debug.Log(e.phase + " : " + input);
    20.        
    21.     }
    22. }
    upload_2021-4-9_20-6-51.png

    when I press 'A' or left arrow (and hold it):
    upload_2021-4-9_20-7-58.png
    its hard to tell but they all happen at the same time

    Important:
    So when I unplug my controller it works just fine. It has something to do with my controller but I don't know what or how to fix it.
     
  4. talha_munity

    talha_munity

    Joined:
    Nov 24, 2017
    Posts:
    2
    This might not be your issue but I had been debugging this issue for a day now so it may help someone else that comes upon this. It turns out it was due to sending key presses over Teamviewer. I'm guessing Teamviewer's method of keystroke capture does not allow for continuous output.

    Holding the key down resulted in the Started and Performed and Canceled callbacks being repeatedly called in that order.

    The solution was to use the Windows on-screen keyboard app to send the key press resulting in only the Started and Performed callbacks being called.
     
  5. Maskimus

    Maskimus

    Joined:
    Apr 6, 2013
    Posts:
    4
    I'm having the same issue.

    The command works fine when i use Keyboard or my steam controller, but when i connect my PS5 controller, a key press will call everything on the first frame on all connected devices.

    Did you manage to find a solution? im wondering if its an issue with the PS5 controller, or do i potentially need to setup a device manager which switches to only 1 active device?
     
  6. Mr-Jun

    Mr-Jun

    Joined:
    Jul 1, 2015
    Posts:
    9
    I'm seeing this strange issue as well. It's as if some phantom is moving the analog stick slightly even when idle. Some other posts said it might have to do with Gyro controls on the PS controllers, but I haven't figured out how to tap into it yet.