0
\$\begingroup\$

I'm working on a 3D game with a spherical collider for the player.

My ground check looks like this, where I'm currently hardcoding Vector3.down as the direction:

Physics.Raycast(position, Vector3.down, out hit, FLOOR_RAYCAST_DISTANCE, PLAYER_GROUND_COLLISION_MASK)

This works for a majority of cases (as does toying with the value of FLOOR_RAYCAST_DISTANCE), but breaks down depending on the speed of the character & the intensity of the slope.

My approach is to tilt the raycast direction dynamically based on the velocity of the player, but all my math so far has fallen apart and hasn't worked, as I'm pretty weak with vector rotations. Can anyone help me figure this out?

See some examples here - in all three scenarios, the collider should be grounded, but it isn't on the slopes. example of grounded colliders

\$\endgroup\$
6
  • \$\begingroup\$ Have you considered using a spherecast, so you can detect places where the "shoulder" of the sphere touches the ground, rather than just the bottom tip? \$\endgroup\$
    – DMGregory
    Jul 5 at 23:54
  • \$\begingroup\$ @DMGregory that seems like a good possible workaround, but I wasn't sure of the performance tradeoffs. The game isn't particularly demanding, but a ray seemed a lot less expensive \$\endgroup\$
    – lase
    Jul 6 at 0:56
  • 2
    \$\begingroup\$ First rule of performance: whenever you say "I'm not sure", measure it. Do not trust your gut about what "seems" expensive - seeming does not ship games. The last time I measured this, a spherecast was only 30-50% more expensive than a single ray cast. So if you needed to do one raycast to determine the angle to tilt, then a second raycast in that tilted direction, you're already paying more than for a single spherecast that solves it in one fell swoop. \$\endgroup\$
    – DMGregory
    Jul 6 at 2:19
  • \$\begingroup\$ @DMGregory right, you should use Spherecast \$\endgroup\$ Jul 7 at 14:33
  • \$\begingroup\$ Thanks for the input @DMGregory - if you want to type it up as an answer I can accept it, or I can answer myself in detail once implemented \$\endgroup\$
    – lase
    Jul 11 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.