- Home /
Jump Normal to Collider Surface
Hi All,
I'm working on my player controller and have everything really smooth and robust except the jumping feature. In an effort to reflect real world physics as much as possible, I'm trying to make sure the player model is able to "jump" in a direction normal to whatever surface they are colliding with. I'm thinking similar to the way Titanfall allows the player to wall-run and jump. However, this poses a couple of problems.
There doesn't seem to be a way to determine if a collider is colliding with anything on demand. This would be useful because all that would be required to implement a jump feature would be to grab the player collider, run a test to see if it has any points of contact and if so, where on the player collider those points of contact are. Once those points of contact are determined, use the vectors to those points from the center of the collider and create a vector that is the sum of those vectors. This vector would dictate the direction that is normal to all points of collision. Then jump in that direction.
I've tried looking up CapsuleCollider methods, raycast methods, OnCollision methods, but I feel like I'm missing how to utilize these to get the functionality I'm desiring. I'm sure using OnCollisionStay would work but that sounds like computational overkill as I'm really only interested in changes in collision state, i.e. when a player stops contacting a collider, check to see if the player has any points of collision; if no, then the player is airborne and can't jump.
My question then is: is there a good way to perform on-demand collision point checking in all directions of a collider?
Answer by Lashes · Oct 14, 2021 at 04:06 PM
Hi - I'm not sure if this would would be a method to use in your specific case, given that you want to check all directions of the collider, but for what it's worth, I used a raycast instead of collision detection to determine if my player could jump, because I realized there may be floating point errors using just strict collision detection. So, for what it's worth, this is what I've come up with (see script below):
(Also, I'm very new to programming -- about a week in now -- so forgive me if this is way off from what you're looking for)
(Also also, I realize this question is 5 years old now, but maybe others have a similar question still...?)
The two relevant functions within my larger 'PlayerController' script look like this:
// to see if player can jump, check for a raycast down, using origin, direction, and max distance (obj center to floor distance):
private void Update()
{
canJump = Physics.Raycast(playerCenterTransform.position, -transform.up, raycastLength);
}
// to determine public raycastLength above, visualize by drawing a line (gizmo) when object is selected in the scene (before runtime):
private void OnDrawGizmosSelected()
{
Gizmos.DrawLine(playerCenterTransform.position, playerCenterTransform.position - transform.up * raycastLength);
}
Anyway, this works for me, and I hope it works for others. And if anyone has any suggestions on improvements, please let me know!
Your answer
![](https://koobas.hobune.stream/wayback/20220612062934im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I jump? 0 Answers
Not detecting collision? 1 Answer