❔ slide bug in csharp script in unity
im making a game in unity and i have a bug in my script where my character glides over the ground when i hold Space + left or right + and then let go of left or right
22 Replies
Maybe it has to do with your code not being consistently formatted and there being loose spaces, brackets and all that 🥹
In all seriousness though, this could very much cause mistakes
Sound slike movement input is not being read if the player jumped
i tried cleaning it up
i have no idea how to fix this i have sat here a couple of hours trying to find solutions
rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue); is possibly wrong in your use case here. When you do this its immediately overriding the horizontal (sliding) momentum by setting a new velocity directly. Its not ideal to set velocity directly unless that's exactly what you need to do.
Instead, use AddForce https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html
The line you want is likely: rb.AddForce(rb.transform.up * jumpValue);
if i replace
rb.velocity = new Vector2(moveInput * walkSpeed, jumpValue); with rb.AddForce(rb.transform.up * jumpValue);
it still does the same thing
I would try replacing all the
rb.velocity =
with AddForce calls. You will need to adjust the multipliers (in this case WalkSpeed) to possibly much bigger numbers as AddForce imparts energy to the rigidbody as though something hit it in X direction
changed them all to rb.AddForce
the player is now on iceskates
Yep thats more 'normal' In the Rigidbody in Unity's inspector, increase the drag
That's probably 0 if you've never changed it
![](https://utfs.io/f/daca0b07-4cf6-4deb-8e6e-177f586fbb0d-1nq2cb.png)
yea
So depending on how you want your character to move, you have to balance the amount of force being added vs the amount of drag. There's quite a few ways to do it so its more about trying different solutions until you get one that feels right
the jump has became very inconsistand and it doesnt move left or right anymore
im trying to figure out why but cant
So there are a lot of things going on in that script, some of which from what I can see of your game isn't required for what you're wanting to do. Give me a few mins I'll write up some example code
ok thanks!
i will be back in ~1 hour
Not as feature complete as yours, it lacks the wall finding stuff. However I think that's doing what you wanted 🙂 You may have to adjust the values in the inspector as I was just doing it with a 1x1 cube