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, 2022 at 21:01

2 Answers 2

2
\$\begingroup\$

Hopefully I can post with zero reputation! I had an issue similar to this one. GetAxisRaw was pegged at 1 on vertical with no input from keyboard and no controllers connected.

I did have a 3Dconnexion spacemouse installed at one point and the utilities for that were installed and running. The spacemouse was not connected.

After shutting down all 3dconnexion utilities and rebooting Unity the issue went away.

All this is to say that if you encounter this issue it is very possible that it is related to software that is installed on your system. it's useful to check on a different system.

I hope that this helps.

\$\endgroup\$
1
  • \$\begingroup\$ Ok this is definitely helpful, I had a 3dconnexion mouse too and disabling the drivers fixed it. \$\endgroup\$ Jul 16 at 6:40
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\$
2
  • \$\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
  • \$\begingroup\$ I'm having the same issue, seems also that pressing ESC to exit mouse focus on play window and focus editor window fixes. \$\endgroup\$ Nov 22, 2022 at 9:28

You must log in to answer this question.

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