I’m trying to make a character that can go on walls and for that the player will change it’s Rigidbody’s up direction to match the normal of the surface it’s walking on. I also want it to “transition” between surfaces, by this i mean that if the character is touching multiple surfaces then set it’s up direction to the average of all the normals. For this I’m using SphereCastNonAlloc.
My problem is that if the player is touching different surfaces of the same object it will only pick one surface at a time, but if it’s touching different surfaces from different objects then it does what i described earlier correctly.
Off the top of my head there are a couple possible solutions.
Separate objects with different surface normals that intersect so that a single spherecast registers multiple hits that you can average. This is probably simpler, but more setup and not as capable. Wouldn’t work on terrain for example.
The better solution is to change to using multiple raycasts in different directions and calculate a normal from the average. Shoot the raycasts like tripod legs (45 degrees down) all around the character so that when they get near a wall or angled surface they hit. Probably want one shooting straight down too.
I tried using the capsule collider’s collision detection before but it does weird stuff around ramp edges. It detects a second collision on the very edge, making the character use the average between the actual collision and that second one that shouldn’t exist. This only happens when using a capsule collider, if using another type like a sphere one this doesn’t occur. (Magenta arrows are the normals of collisions it’s detecting and the blue line is the average)
If you lerp the character’s current rotation to the target rotation then any irregularities will be smoothed out. But if it still bugs you then you may need to start making your ramps from a single piece of geometry so as to avoid any cracks where sections meet or intersect each other.
Scenes made from multiple pieces of connecting geometry often result in physics related issues known as ghost collisions. Your issue demonstrates the root cause of ghost collisions.
Lerp sounds like a pretty good idea, thanks for that. About making the ramps from a single piece, i already did that i think, i used ProBuilder and just extruded one of a plane’s edges.
I just did a little test and created a mesh ramp and imported it. I was surprised to see your issue still occurring. It’s only when I set the ramp’s mesh collider to Convex that the issue went away. I’ve never used ProBuilder and so I don’t know what your options are when it comes to creating levels that use convex mesh colliders.
I tested it and it’s true, the issue disappears if the mesh is convex. This is a pain in the butt because it means i (obviously) can’t use non-convex meshes which are very quick to set up and I can’t use very complex meshes because ProBuilder simplifies them automatically when making them convex, filling any hole on their surface (like in the image).