Search Unity

C# GetInput("Horizontal");

Discussion in 'Getting Started' started by Vikaas_A, Aug 2, 2021.

  1. Vikaas_A

    Vikaas_A

    Joined:
    Aug 1, 2021
    Posts:
    2
    Hello, I am new to unity. This c# code I watched it youtube tutorial for dynamic moment of object. But it stic to the position not moving a inch. Then I see console output for "float h = Input.GetAxis("Horizontal");" its display zero. What's problem in my code.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Move : MonoBehaviour
    {
    // Start is called before the first frame update
    public float speed = 5f;
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");
    Debug.Log(h);
    Vector2 pos = transform.position;
    pos.x += speed*Time.deltaTime;
    pos.y += speed * Time.deltaTime;
    transform.position = pos;
    }
    }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Unity has been monkeying with the Input APIs recently, because they hate stability and get squirrely whenever developers start getting comfortable with their APIs. So, it could be your project is set up to use the "new input system" instead of the "legacy input system". The new one is a lot more complex, and such simple code as the above will not work.

    I believe you can tell the difference by opening the Package Manager, and looking for the Input System package. If it's there, delete it, and then update your project settings as described here.

    The next thing to check is that you actually have a Horizontal axis set up in the Input Manager window. Though if you don't, you should be seeing errors in the Console.
     
    april_4_short likes this.