My character is constantly moving left

Hi! Im new to unity and i wanted to just make my character going right and left. Unfortunately, even though i’ve written exactly the same as in the tutorial, my character keeps sliding left with great speed.
Here is the code:

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

public class Test : MonoBehaviour
{
    Rigidbody2D _rb;

    float _walkSpeed;
    float _inputHorizontal;

    // Start is called before the first frame update
    void Start()
    {
        _rb = gameObject.GetComponent<Rigidbody2D>();

        _walkSpeed = 5.5f;
       
    }

    // Update is called once per frame
    void Update()
    {
        _inputHorizontal = Input.GetAxisRaw("Horizontal");

        if (_inputHorizontal != 0)
        {
            _rb.AddForce(new Vector2(_inputHorizontal * _walkSpeed, 0f));
        }
    }
}

and here is the character named Player

Also new here. I’ve found scattering Debug.Log() around helps me identify the problems easier. On my viewing, I think I’ve spotted the problem.

    void Update()
    {
        _inputHorizontal = Input.GetAxisRaw("Horizontal");
        if (_inputHorizontal != 0)     //< _inputHorizontal could be null or any other non-zero and still affect the movement
        {
            _rb.AddForce(new Vector2(_inputHorizontal * _walkSpeed, 0f));
        }
    }

consider _Debug.Log(inputHorizontal) to check if it is actually 0 when you don’t use it.

thats the output i get when i don’t press anything. For some reason tho, when i press right arrow, the output is 1. do you have any idea of how to remove the previous input?

EDIT:
I have retracted exactly the same value that is being inputed and it works perfectly fine, but the input is still there

You shouldn’t need to remove the previous input, it’s weird to me that it has the same value every frame. It should be exactly 0 when there’s no key pressed, exactly -1 or exactly +1 if left or right are pressed.
Do you have only keyboard, no gamepads etc attached? I’m not sure if GetAxisRaw applies a deadzone to stick inputs (probably not?); although -0.78 is huge, way outside of normal deadzone range. I wonder if something weird has ended up in your InputManager settings?

I think you should do the _rb.AddForce from FixedUpdate btw, otherwise the force you’re adding will vary with framerate.

I have had my joystick plugged, after disconnecting it, the output when i dont press anything is -1

That’s also pretty weird, the output when you don’t press anything should be 0.
Have you modified your Input manager settings somehow? Like, the defaults from a new project are good as a beginning. E.g., an empty project, if you just run a script that does Debug.Log(Input.GetAxisRaw(“Horizontal”)) you should see -1, 0, 1 for left, nothing, right.

1 Like

Use the New Input System to rule out the old Input setup having weird setup or issues.

It sounds like you have some kind of hardware drift or deeply incorrect calibration outside of Unity that is causing bad values, but the Input could be configured wrong too. Either way, you should move to New Input System, it’s generally better across the board.

1 Like

thanks, when i just redone my project it worked, idk, some wierd bug, but thanks for all your help