- Home /
Stop character controller from detaching from ramp
I have the following line of code:velocity -= Vector3.Dot(collision.normal, velocity) * collision.normal);
This line of code runs whenever OnControllerColliderHit() is called. Essentially, this is just Newton's Third Law for character controllers. The purpose of this is to slow the player down when they bump into walls, for realism. I do not think this is the root of the problem, but it might be important to consider when fixing.
I do not know why, but when going UP ramps, after letting go of movement keys the player's velocity.y increases.
In the following image, the player is walking up a ramp at a constant rate: In the next image, the player is no longer holding down the forward key/is not trying to move anywhere. As soon as the player lets go of w, a, s, and d, the player's vertical velocity changes to a positive number instead of the player just staying in place on the ramp:
My question is how do I stop the player from moving upwards when they shouldn't be or improve the script to stop this from happening? I need the player to slow down against walls appropriately, but I also need this bug not to occur. Other important variables include: The character controller's step offset is 0, skin width is 0.01, and slope limit is 1. I replicate gravity by applying a constant downward force of 20 per second. When I remove the velocity change upon collision, this still happens. What is causing the player to move upwards when moving against ramps/slopes?
Unity already implements bounce, drag, and most of what you might need. Why not just use a physics material with the right drag ins$$anonymous$$d of your formula?
because you can't apply physics materials to character controllers...
Oh, right. A character controller is not the best choice if you want advanced physics. We can't help you without the rest of the code.
You're saying it still happens when you get rid of the code at the top of the post?
What is the exact code that you're using for simulated gravity?
In a situation like this the best thing to do is to isolate the behavior in a new scene and slowly add elements until you can find the culprit. This takes some patience because you can only add one element at a time. Try this out and see if you can find the culprit, then respond here and we'll see if we can't suss out what's happening. $$anonymous$$y hypothesis is something to do with your simulated gravity, but only you can debug for that using the method I've described here.
Nah, I'm saying it happens because of that code. I actually figured it out for the most part. I had the player's velocity decreasing by 20 per second on the y axis, but only in certain conditions, namely whether or not grounded. I just made it a constant force; since the third law replicates realism, video game physics colliding with it created the bug I mentioned. In other words, my issue is solved and the solution was to move the gravity into fixed update.
Answer by xthunderduckx · Jan 27, 2020 at 10:50 PM
Figured it out. The line of code I had was fine, and appropriately applies force in the necessary direction in order to replicate newton's third law. The issue was that my gravity was only being applied in certain conditions, preventing the player from losing vertical speed in said conditions, and thus creating the error. I moved the gravity to fixed update and all is well.