Input.GetAxis() not returning -1, 0, or 1

Is there any reason for the numbers to be, close, but not -1, 0, or 1?

When I hit play in Unity, deltaX’s value is 0, as it should be, then 1 when I press and hold Horizontal’s positive key, as it should. Then -1, as I press and hold Horizontal’s negative key, as it should.

But repeating the ship’s movement, the numbers never land on those values again; they’re close, like 0.00330969 and 0.9722582, but not 0 and 1. Rick’s video was able to produce the correct values each time as the appropriate keys were pressed and let go.

It’s 2 lines of code here, what’s going on?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // [SerializeField] float moveSpeed = 10f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    private void Move()
    {
        float deltaX = Input.GetAxis("Horizontal"); //  * Time.deltaTime * moveSpeed;
        Debug.Log(deltaX);
        // var newXPos = transform.position.x + deltaX;
        // transform.position = new Vector2(newXPos, transform.position.y);
    }
}

Hi,

Yes, that behaviour could be normal. What input device do you use?

Oh okay. Just the keyboard, A and D for left and right.

In this case, GetAxis should return 1, 0, and -1. I would suggest to test GetAxisRaw, which is perfect for keyboard input.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms