I have a problem with my rigidbody where it will pull slightly down the ramp when facing the top of the ramp and walking straight sideways. I want it so that while on a ramp, if the player strafes, or moves any direction on the ramp, that the slope of the ramp does not get added to the forces on the player, so when they are facing the top of the ramp and move sideways, they move directly sideways.
Is there an easy way to do this, or a common solution to this? in most games I notice that moving sideways does not also move you slightly downwards as well.
Any suggestions?
Thanks.
Hard to say without knowing your exact setup but in my rigidbody controller I handle gravity manually, and only apply it when the controller is not grounded. I also move the controller up/down when within the step offset range so it sticks to ramps and doesn’t “fall” down them.
A more general solution could be clamping the rigidbody’s movement on the X and Z axis using the movement input as the basis for the clamp.
The reason it doesn’t happen in other games is that most games don’t use rigidbodies for player characters, as they’re unstable and run on the physics timestep, meaning that if you use it for say an FPS game you will get visibly lower framerate when moving unless you greatly increase the physics timestep (which generally is a bad idea). Even if the visibly lower framerate doesn’t matter to you, you’ll also have less responsive input as the game has to wait until the next physics timestep to apply your inputs to the rigidbody. That combined with say a wireless controller makes for visibly high latency.
The best solutions I’ve found are either to just use the Character Controller component or to use a heavily controlled rigidbody with things like gravity and velocity being handled entirely in code, and the rigidbody itself being used mostly only for collision detection and affecting other rigidbodies. You can make your camera a separate object that discreetly and smoothly moves to the desired location using the standard Update so that the game does not appear stuttery when moving around like if you were to just make the camera a child of the rigidbody. Doesn’t help the input latency issue but solves a much more visible and annoying issue.
A more involved solution is using the Physics.OverlapBox function recently added to make your own character controller from scratch that would work much more reliably but wouldn’t automatically handle physics interactions. If your game does not need the player to interact with physics objects around I highly recommend using this or the Character Controller instead of a rigidbody.
Bonus fun fact: A rectangular prism may seem weird to use instead of a capsule for a character controller but in reality its great because it allows for precise movement up stairs as long as you treat the controller collider as a point of reference and not a solid object that needs to glide over things. The logic is that you take all the collision points that fall in the bottom “step” area of your controller and move it up accordingly. Any points above the step area should count as “walls” and push the player back relative to the normal the point of intersection falls on. There’s other checks you have to do like checks for ceilings and whatnot, but that’s the basic idea. Half-Life 2 for example uses a non-rotating scaled box collider for the player and you’d never even notice if you were just playing the game.
Hope this helps and sorry for the massive wall of text, it’s just an issue I’ve been tackling for years on my game so its pretty burned into my memory haha
This is great input, I would love to take the route of the character controller, but my project is so heavily integrated with the rigidbody with the player abilities and animations that it would be an entire revamp, which is what might have to happen, but only after I am certain I cannot achieve the correct behavior without doing so.
I am going to try to limit the forces applied when grounded and see if that helps. I am also considering applying an upward force to the player when they are on an incline to negate the forces applied by gravity.
I am hoping one of the two works, and really hoping I don’t have to revamp the whole engine.
Thanks again for the reply it helped.
Also, regarding your statement: “I also move the controller up/down when within the step offset range so it sticks to ramps and doesn’t “fall” down them.” I would love to see a snipit of the code that handles that if willing.