Reputation: 1752
I made a function to anticipate collision with a CapsuleCast because my character is going fast and a lot of collision are not detected by unity:
private bool CheckForwardCollision(Vector3 direction)
{
RaycastHit hitInfo;
float maxDistance = m_DefaultSpeed * Time.deltaTime + m_Capsule.radius;
int layer = 1 << LayerMask.NameToLayer("Default");
// Debug CapsuleCast
DebugExtension.DebugCapsule(transform.position + (direction * maxDistance), transform.position + (direction * maxDistance) + (Vector3.up * m_Capsule.height), new Color(1, 0, 0), m_Capsule.radius);
// Draw last collision point
DebugExtension.DrawPoint(debug_hitpoint, Color.cyan);
// Get collision with a capsule cast which is in front of character
if (Physics.CapsuleCast(transform.position, transform.position + (Vector3.up * m_Capsule.height),
m_Capsule.radius, direction, out hitInfo, maxDistance, layer, QueryTriggerInteraction.Ignore))
{
debug_hitpoint = hitInfo.point;
return true;
}
return false;
}
But it collide with object which are higher than the height of the capsule. Here is an example:
You can see the hit point which is higher than the capsule.
What is wrong? Thanks
Upvotes: 0
Views: 1159
Reputation: 1752
After many hours of researching, i found the solution:
Physics.CapsuleCast(point1, point2, ...)
and DebugExtension.DebugCapsule(point1, point2, ...)
are not made on the same way. On DebugCapsule, point1 and point2 are the point to the extremity to the capsule, whereas on CapsuleCast, thoses points are the start of curve part.
So the solution is to remove the capsule's radius from the point1 and 2!
Vector3 point1 = transform.position + Vector3.up * m_Capsule.radius;
Vector3 point2 = transform.position - Vector3.up * m_Capsule.radius + (Vector3.up * m_Capsule.height);
Physics.CapsuleCast(point1, point2, m_Capsule.radius, move, out hitInfo, maxDistance, layer, QueryTriggerInteraction.Ignore)
Upvotes: 1