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\$
0
\$\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 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.