- Home /
Unity input system only triggers once
Hi there, I recently started work on a platformer, and wanted to use the new Unity input system. Unfortunately, after setting it up, any input only triggers once, and holding down on buttons or keys does not register. It all worked with the old input system, but for some reason when I hold down a key, this is the result: Ideally, as I hold down a key, the character would continue to move instead of just move a little.
As you can see, I have everything set up properly: I am not using any interactions, and I am on Unity 2019.4.14 LTS.
Here is my code:
// Move
public void OnMove(InputAction.CallbackContext in_context) {
// Apply horizontal movement
float temp_movementDirection = in_context.ReadValue<float>();
rigidBody2D.velocity = new Vector2(temp_movementDirection * speed, rigidBody2D.velocity.y);
}
Any help would be awesome!
No luck :/ - It shouldn't make a difference anyways, since I'm getting my values by specifying the type as a float.
Answer by Zubzuke · Nov 14, 2020 at 12:41 AM
The Player Input component is provided for convenience and it's a mess. You should forget about it and deal with the new input system on your own, it's a world easier:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class MyInputManager : MonoBehaviour
{
public float movementDirection = 0f;
// this is the class associated with the action map
private PlayerControls controls = null;
private void Awake()
{
controls = new PlayerControls();
}
private void Start()
{
// setup event handlers
controls.Gameplay.Movement.started += Movement_started; // key down event
controls.Gameplay.Movement.performed += Movement_performed; // key up event
controls.Gameplay.Movement.canceled += Movement_canceled; // kinda same thing as performed
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
private void Movement_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
movementDirection = obj.ReadValue<float>();
//Debug.Log("Movement action started.");
}
private void Movement_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
//Debug.Log("Movement action performed.");
}
private void Movement_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
//Debug.Log("Movement action canceled.");
}
}
So, I opted for this originally, and as far as I can remember it was working (it's been a slow week), however then I found out how easily local multiplayer can be added in if I use the player input component in unison with the player input manager component, and I switched. In fact the multiplayer aspect seems to be already working, just not the crippling problem I've brought up in this thread. Do you know if there's still easy implementation for local multiplayer I can use in runtime?
But you're totally right, I should probably switch away from the player input component if possible.
You could look through the input manager package (and samples) and see what you can use for your own input manager.
But if you wanna stick to the predefined input helpers, you may have to use a bool to keep track of action status, check this thread:
https://forum.unity.com/threads/new-input-system-how-to-use-the-hold-interaction.605587/
Answer by ahsen35813 · Nov 13, 2020 at 11:36 PM
I can't help you with the GUI based coding, but I can tell you that in C#, you need to use Input.GetKey(Keycode.Example)
rather than Input.GetKeyDown(Keycode.Example)
Yeah, thanks for pointing that out, everything worked with the old input system. This is the new Input System found in the package manager. I'm not using any GUI based coding...
Oh I'm really sorry about that. I've never seen this before, so I assumed it was the new drag and drop coding system
Your answer
Follow this Question
Related Questions
Gamepad and keyboard act differently in multiplayer game using Input System 0 Answers
How to find out if a button has been released (new input system/gamepad) 0 Answers
Multiple Players on one Computer/Console 5 Answers
Switching between Inputs 1 Answer
What Key do you press to "Jump" in the T_T Tutorial? 2 Answers