Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug New Input system calls function on startup

Discussion in 'Input System' started by EeeVeeVa, May 5, 2023.

  1. EeeVeeVa

    EeeVeeVa

    Joined:
    Jan 5, 2023
    Posts:
    2
    I just started a new 2D project, imported the input system and made simple movement scrip.
    I added a rigidbody of course and a collider 2D.

    On startup in the editor, or even after i built the game, at the start of the game: The player moves slightly at the start for a short duration. Specifically to the bottom left of the screen.
    Note that this only happens roughly 20% of the time.

    When it does happen i get a debug.log from the onmove function, which shouldnt be called unless im pressing a key, and when it stops shortly after (lasts like 0.1 - 1 seconds) i get a second debug.log.
    My keyboard isnt giving any input, as it kept happening after i unplugged it, and it happened to friends that i sent the built to.

    upload_2023-5-5_11-26-33.png

    Here is the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     [SerializeField] private float moveSpeed = 2f;
    7.  
    8.     private Vector2 moveDirection;
    9.     private Rigidbody2D rb;
    10.  
    11.     private void Awake()
    12.     {
    13.         rb = GetComponent<Rigidbody2D>();
    14.     }
    15.     private void Start()
    16.     {
    17.         rb.velocity = Vector2.zero;
    18.     }
    19.     private void OnMove(InputValue value)
    20.     {
    21.         Debug.Log("moving");
    22.         moveDirection = value.Get<Vector2>();
    23.     }
    24.     private void FixedUpdate()
    25.     {
    26.         rb.velocity = moveDirection * moveSpeed;
    27.     }
    28. }
    The start function which sets the volocity to zero was an attempt to fix this issue but it didnt do anything as i realized that the bug was because onmove gets called, why does it get called? I just started this project so its pretty barebones and pretty much only has what i listed, there are no other scripts in the way. This exact bug has happened to me before in a 3d project as well.

    (Using unity 2021.3.24f1)