Hi,
I recently tried to implement a simple 3D character controller to move around my scene. Horizontal movement and jumping work fine (I used an OverlapBox for the ground check).
My big issue is when running next to a slope. While my capsule character collides with the beginning of the slope, he is thrown away in the air. The video below shows my issue, note that I don’t press any key to jump I just move forward.
I think it has to do with the character collider entering the slope collider and then thrown away to respect physics. But I just want it to not move in or the character to be adjusted just above the slope without any jump.
Do you have any idea how to fix that ?
Thanks
Okay, I solved my problem. This wasn’t due to the collider but to my script.
Each update, I was adjusting my vertical movement with the current character’s velocity.y. To calculate the vertical movement, I was taking the current velocity.y, and then applying gravity. The problem was that while bouncing up with the collider, the velocity was very high. Instead I used a private variable to store the vertical velocity and everything works fine now.
If you use the built-in character controller, I believe this logic is already managed for you.
If you want a capsule controller with physically plausible control, you can use this simple formula to calculate what the character’s vertical velocity should be to reach a particular height (relative to his starting height) at the peak of his jump:
float gravityMagnitude = Physics.gravity.magnitude;
float jumpVelocity = Mathf.Sqrt(Math.Max(0, 2*gravityMagnitude*jumpPeakHeightDifference));
(Note: it assumes that “height” is defined as distance travelled in the direction opposite to gravity.)