Spherecast is not detecting an object

Hello,

I am having trouble with my grounded check for a character controller. I will need to have moving platforms in the game, including moving ramps. However, the player will be parented to the same object the ramp is parented to. So locally, I don’t think they’re moving relative to each other.

The problem is, if the ramp is moving towards the player that’s on it (shown below), the player will slide down the ramp because the grounded check is not returning true, when it should. I’m checking grounded with a spherecast. The spherecast starts towards the top of the player and moves down to slightly below the player. This works great when the objects are not moving, but when the ramp is moving, apparently it’s not detecting it. I’ve included this picture to show the direction of movement that causes problems, the start of the spherecast (blue) and the end of the spherecast (red).

We can see that the spherecast is inside the ramp, but in this frame, it did not return true. When the ramp is not moving, this is what it looks like:

Any ideas why this is not detecting the spherecast hit?

The use of spherecasts or any other form of non-linear-raycasts for character colliders is fraught with “already-intersecting” issues, thus they should be replaced by regular raycasts.

One way to go about this is to do a raycast (not: spherecast!) from a position that’s guaranteed to be inside the character’s collider. You can then use the hit position to perform a distance check - if that’s less than whatever the sphere’s radius would be, then you know without actually using a spherecast if the hit was inside the sphere or outside.

You can still do additional casts as spheres but only after a raycast.

The spheres in the image are drawn with Gizmos using the actual cast locations? If you parent the player (not recommended since physics will ignore parenting anyway) you have to use world locations.

Seeing the code would help. :wink:

Generally I recommend avoiding custom character controllers. Use the Kinematic Character Controller (free) because this perfectly handles moving platforms.

1 Like

Hello CodeSmile! Always nice to see your responses.

I created a custom character controller very early in my project. I landed on a spherecast because the raycast was causing a problem, but I have since forgotten what the issue was. I’ll try out raycast again and consider using the kinematic controller.

Thanks for the input!