- Home /
How to calculate distance between sphere and tangent plane for determining grounded state?
I need to cast a series of rays from the underside of a capsule collider which will determine how grounded said collider is for a 3D platformer character (it is not a character controller, it is a rigidbody). I have set up a for loop inside of a for loop like I would do with a box collider, but now I need to determine the Y distance between the very bottom of the collider and the surface of collider at each x and z grid point. The code would go in the y section of rayOrigin's new Vector3.
public float rayDist = 0.1f;
public int rayLoops = 4;
Rigidbody rb;
Collider col;
Vector3 size;
Vector3 colCore;
bool CheckGrounded()
{
bool yep = false;
//create bugfixing layer mask
int layerMask = 1 << 8;
layerMask = ~layerMask;
int rayCon = rayLoops * 2;
float colRad = size.x;
for (int xLoop = -rayLoops; xLoop <= rayLoops; xLoop++)
{
for (int zLoop = -rayLoops; zLoop <= rayLoops; zLoop++)
{
Vector3 rayOrigin = transform.position + new Vector3( ((size.x / rayCon) * xLoop),-(size.y / 2),((size.z / rayCon) * zLoop) ) - colCore;
bool testGrid = Physics.Raycast(rayOrigin, -Vector3.up * rayDist, rayDist, layerMask);
if (testGrid) { yep=true; }
Debug.DrawRay(rayOrigin, -Vector3.up * rayDist, Color.red, Time.deltaTime, false);
}
}
return (yep);
}
I understand there may be a simpler solution that I'm missing, but I'm missing it, so I need to know how to adjust this code.
Your answer
Follow this Question
Related Questions
2D platformer player intersecting floors and walls 2 Answers
2D 360 degress platformer example needed 0 Answers
Rigidbody2D.AddRelativeForce not working 1 Answer
Capsule collider effect for a custom kinematic player controller script 0 Answers
Diagonal Raycasting to detect platform returns true, even when false 1 Answer