0
\$\begingroup\$

this is extremely frustrating, basically Input.GetAxisRaw only ever returns a 1 or -1. see my code below

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

public class player : MonoBehaviour
{
    private BoxCollider2D boxCollider;

    private Vector3 moveDelta;

    private void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
    }

    private void FixedUpdate()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        // Reset moveDelta
        moveDelta = new Vector3(x,y,0);

        // Swap sprite direction, whether you're going right or left 
        if(moveDelta.x > 0){
            transform.localScale = Vector3.one;
        }
        else if(moveDelta.x < 0){
            transform.localScale = new Vector3(-1,1,1);
        }

        //Make this thing move!
        transform.Translate(moveDelta * Time.deltaTime);

        Debug.Log(x);
        Debug.Log(y);

    }
}

Here are screenshots from the input settings, I haven't changed anything:

Horizontal Input Setting

Vertical Input Setting

Please help, I am new to Unity. Could not find anything that work, the other related post was unanswered.

\$\endgroup\$
1
  • \$\begingroup\$ did you find any solution to this problem? I'm having the same one, character starts moving with out any buttons being pressed but when i stop and press play again it works fine. The code have been working fine for a few months and a week back its been acting strange. I tested an older project with the same results. What Unity version are you using? I'm on 2021.2.7f1 Took a break from writing to try something and noticed that both projects with version 2021.2.7f1 has the problem and my other one on version 2020.3.18f1 is working fine. \$\endgroup\$ Feb 3 at 21:01

1 Answer 1

1
\$\begingroup\$

If you are polling the keyboard, InputGetAxisRaw will always only return -1, 0 or 1 per the docs. Use InputGetAxis instead, assuming you don't want to do your own smoothing.

I've tested your code, zero does get returned when no keys are pressed:

console

Try polling/ logging Input.GetAxis() instead of Input.GetAxisRaw to see what kinds of values your keyboard is returning when idle. My guess is that it is returning some very small, but non-zero, value. If I'm right, you'll want to change your logic to move the sprite when the axis value is not simply greater than zero, but greater than that small threshold.

\$\endgroup\$
1
  • \$\begingroup\$ Hi, so as long as I use Input.GetAxis() or Input.GetAxisRaw() it only ever returns a -1, or 1, even on GetAxis() it never returns a 0. I suspect it is hardware related but i disabled any unneeded input devices and it still doesn't work. A really interesting case happens though when I stop the game and then restart it, all of a sudden it does work. But if I make any changes and restart again, I get the error again. \$\endgroup\$ Oct 18, 2021 at 0:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .