GameObject moving without input

Hi.
This is the first time i use the forums, so sorry if i posted it in the wrong place. Im working on a simple top-down wave shooter game right now. The problem is that the player moves against the top right corner without any input when i hit play. I can still control the player (in this case still a cube) by moving in the opposite direction, but once i release the S and D keys it starts drifting left and upwards again. I have the same problem in a 2d platformer i made a while back. It began a few months ago, but it disappeared when i changed to the newer 2019.4.1f1 version. Today it appeared again. I tried changing to the 2020.1.0f1 version since it worked before, but its still there. Sometimes the problem randomly dissapears for a few days, but it does always come back. This is the code i use for movement:

rb.AddForce(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed);

I have a lot of drag on the player to counter the slippery feel it has otherwise. I dont know if that has anything to do with it. Am i doing something wrong or is it a bug?

Yeah, this is a common issue. I’m not completely sure why it happens myself. Even if you figure it out, sometimes your object will get hit by something, and you’re in a constant spin, or push (like you’re experiencing now).

What I do is quickly freeze the rigidbody, and then unfreeze it. That seems to clear out any funk that it may have picked up. This is only helpful if you object is standing still, or you want it to stand still.

GetComponent().constraints = RigidbodyConstraints.FreezeAll
GetComponent().constraints = RigidbodyConstraints.None

Documentation:

Good luck!

I’ve never seen a rigidbody just drifting along on its own without some force being applied to it, or a fair bit of inertia from some earlier force.

What is your input device? Is it possible you’re using a controller and you haven’t configured the dead zones on the sticks? While this drifting is happening, you should try printing what you’re getting for your Input.GetAxis calls, to make sure they’re both 0. Without knowing more about the specifics here, I’d just have to assume you’re getting a tiny bit of unexpected input.

Debug.Log on the values of “Input.GetAxis(“Horizontal”)” and “Input.GetAxis(“Vertical”)” to check that they are actually at 0 when you are not pushing anything.

If they are not at zero, then check if you have any controllers plugged in (ps3/4 controllers, joysticks etc)

Now when i think about it, i do actually have a cheap HOTAS plugged in for flight sims. And if i recall correctly, the stick in the cockpit has always been leaning left when im in some game. Im definietly going to plug it out. That might also explain the randomness of my problem, since i do not always have it plugged in. Thanks guys!

Edit:
It worked!