Why does my character keep moving to the left, even though no input is being used?

Hello! I recently used another code for my fps game that I’m making. I have one slight issue and I believe that it may be picking up an input device, but nothing is being used. Below is the code for my movement, it used to work before hand, but now it literally wont stop going to the left. I’m sure it is picking up an input device, but I thought I’d put it here to get any feedback. Thanks!

using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    //Variables
    float moveForward;
    float moveSide;
    float moveUp;

    bool isGrounded;

    public float walkSpeed = 1f;
    public float sprintSpeed;
    float currentSpeed;
    public float jumpSpeed = 5f;
    Rigidbody rig;

    //Start is called before the first frame update
    void Start()
    {
        rig = GetComponent<Rigidbody>();
    }

    //Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            if (currentSpeed != sprintSpeed)
                currentSpeed = sprintSpeed;
        }
        else
            currentSpeed = walkSpeed;

        moveForward = Input.GetAxis("Vertical") * currentSpeed;
        moveSide = Input.GetAxis("Horizontal") * currentSpeed;
        moveUp = Input.GetAxis("Jump") * jumpSpeed;
    }

    private void FixedUpdate()
    {
        rig.velocity = (transform.forward * moveForward) + (transform.right * moveSide) + (transform.up * rig.velocity.y);

        //Character jumping
        if (isGrounded && moveUp != 0)
        {
            rig.AddForce(transform.up * moveUp, ForceMode.VelocityChange);
            isGrounded = false;
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        //Collision with ground
        if (collision.gameObject.tag == "Ground")
            isGrounded = true;
    }
}

Could be input drift (eg, joystick not returning (0,0) when centered.

Could be physics pushing it around (like gravity, colliders or other slope forces)

Could be something else.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thank you! Where do I put the Debug.Log? Next to the moveSide= Input.GetAxis…? Or somewhere else?

Wherever you want to find out a value!

The first thing I would do is look to see if your inputs truly are zero. You might want to print them with custom formatting so you can see more digits of precision.

https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=net-6.0

If that’s the problem you can go adjust the deadband (called Dead) in your input settings, or else clamp all smaller values below a certain amount to just be zero.

1 Like

Figured out the issue, I had a handbrake that was still connected and was for some reason picking up as input!

I hate myself I think I spent 3 days trying to find this bug