- Home /
How to get the start, end, radius and direction of a CapsuleCollider in world space?
I have a nested prefab which has a capsule collider
parent (rotate x:45, z:45, scale 0.5F, position: 3,709441, 0, -0,05440593)
-child (rotate x:15, z:15, scale 0.5F, position 0,0,0) with a CapsuleCollider
CapsuleCollider
- center (-0,01, 0,4362606, -0,004439712)
- radius (0,8627189)
- height (1,910243)
- direction (Y-Axis)
Resulting values
- start (3,684777, 0,07986824, -0,03415439)
- end (3,67282, 0,1229746, -0,02260407)
- diameter 0,4313595
and I am trying to get the capsule colliders values (start, end, direction, radius, height) in world space.
I use:
var c = capsuleColliders[i];
var dir = c.direction == 0 ? X :
c.direction == 1 ? Y : Z;
dir = c.transform.TransformDirection(dir);
var center = c.transform.TransformPoint(c.center);
var height = c.height;
height *= c.direction == 0 ? c.transform.lossyScale.x :
c.direction == 1 ? c.transform.lossyScale.y : c.transform.lossyScale.z;
var radius = c.radius;
radius *= c.direction == 0 ? c.transform.lossyScale.x :
c.direction == 1 ? c.transform.lossyScale.y : c.transform.lossyScale.z;
var e = height / 2F - radius;
var start = center - dir * e;
var end = center + dir * e;
However, the radius looks correctly scaled, but the direction is not correctly transformed to world space. The green spheres should align perfectly with the capsule colliders top and bottom sphere as they are drawn at start and end with a radius equal to the capsule radius.
When I set the parent rotation to 0 the drawn spheres align perfectly.
Therefore I assume that dir = c.transform.TransformDirection(dir)
does not consider the parents rotation.
How can I get the correct start, end, height, radius of a CapsuleCollider in world space?
How do you draw the spheres? Gizmos? What are the values you pass in?
I've set up a small project and tried to reproduce the issue, but your code appears to work just as expected (for me) and correctly draws the Gizmo-Spheres at the desired positions (start, end, both with the correct radius). Even when I change anything on the setup during runtime, the spheres are always drawn at the correct positions.
Ok wow, that is something I did not expect. Thank you for trying to replicate it. I simply create two spheres with create primitive and set position (start, end) and scale (2*r).
Oh god, you are correct. The bug was, that there was a disparency between the rotation from the spawned gameobject transform (prefab.transform.rotation) and the transform used to calculate the CapsuleColldier (Quaternion.identy). Therefore the rotation obviously was wrong because what was shown was not what was used durign the calculation. Thank you so much, because you said it works at your project, I was confident enough that the code above is correct and that there must be some error somewhere completly else!
I would like to confirm, that your question is the correct answer.
Useful in case where you want to use
Physics.CheckCapsule(start, end, radius);
Thank you very much for sparring me many hours of tinkering