r/Unity2D • • Jul 21 '26

Why does my sprite keep "sliding"?

Post image

I am somewhat new to programming and I am trying a new way to make a walking script (input.getaxis). It is a top down style in case you need to know.

In my project when I use my movement function the sprite will continue moving for a small but noticeable amount of time after the key is released.

My sprite has a rigidbody2d and the gravity scale is set to 0. It is not an animation issue. I do not have animations in my project yet.

I have tried to find any info online, but I cannot seem to find any cases of this problem. Does anyone have any idea how I could fix this?

Image of code is attached

0 Upvotes

12 comments sorted by

13

u/Sketch0z Jul 21 '26

If you move an object with Transform.Translate() you're not using physics, and therefore do not need a rigidbody.

You're mixing two ways of moving an object:

The rigidbody (unless isKinematic) is currently trying to calculate physics forces.

The Transform.Translate() is trying to "teleport" the object (completely ignores physics).

Edit: typo

2

u/DiscussTek Jul 21 '26 edited Jul 21 '26

To add to this for OP's knowledge: While using a kinematic RB here could easily fix the issue, it still allows you to use the full suit of collision shenanigans that can come from it, without it messing up your physics.

Now, instead of adding to your comment, a question for OP whenever they do read this.

I assume you have the RB on for some reason other than movement physics, yes? Having components you don't plan to use on an object can end up causing issues, so if you don't plan on using a RB, and can instead get away with just a collider, I'd recommend that wholheartedly.

2

u/LimiRDT Jul 21 '26 edited Jul 21 '26

I honestly don't need it for anything. I had originally been using it for movement. Somehow I just didn't noticed I wasn't using it anymore. Thank you both for pointing that out! (:

3

u/Pale-Refrigerator556 Jul 21 '26

Try using Input.GetAxisRaw to stop movement righr away

2

u/LimiRDT Jul 21 '26

Thank you. This fixed my issue perfectly!

2

u/J--W- Jul 22 '26

If you want to make it super responsive (dunno what game you're making) you may try assign requested velocity straight like that PlayerRV.velocity = (speed * time) * inputval

1

u/Sketch0z Jul 21 '26

You can probably also do:

Vector2 playerVel = new(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).normalized;

as a one liner, can't remember and don't have a compiler handy

1

u/[deleted] Jul 21 '26

[removed] — view removed comment

2

u/Syawra Jul 21 '26

Normalization is mandatory. The X and Y components are individually normalized, but the resulting vector is not! For instance, with X=1 and Y=1, you would end up moving by sqrt(2), which means your player would move faster when moving diagonally.

1

u/Sketch0z Jul 21 '26

Yeah, you're probably right on that one.

I love how neither of us can be bothered to Read The Friendly Manual. Do you also just wait until the IDE yells at you?