Hi,
I have a capsule (invisible, without collider attached as a child to my camera). This was added in editor.
I am planning to use CapsuleCast for certain collision related actions.
My idea is, whenever I want to call capsule cast, instead of calculating the start and end of capsule to be used for CapsuleCast, I will just get them from the capsule primitive I attached to my camera and pass them to the CapsuleCast method.
Is it possible to get the details (Start, End, Radius) of the primitive I added in the editor, during game runtime?
Please let me know if my question/wording is not clear. Your answers are much appreciated.
Thanks.
Its possible to calculate these values (start, end) because the “radius” you already have.
To easier visualization, use this free asset called Debug Extensions that will be easier to you to draw it inside an OnDrawGizmos() monobehaviour method.
Store a reference to your CapsuleCollider on your MonoBehaviour script, than use this:
public CapsuleCollider CapsuleColliderRef;
private void OnDrawGizmos()
{
Vector3 startCapsulePos = transform.position + new Vector3(0f, CapsuleColliderRef.height/2f) + CapsuleColliderRef.center; /*this line gives you the capsule start position*/
Vector3 finalCapsulePos = transform.position - new Vector3(0f, CapsuleColliderRef.height/2f) + CapsuleColliderRef.center; /*this line gives you the capsule end position*/
DebugExtension.DrawCapsule(
startCapsulePos,
finalCapsulePos,
Color.blue,
CapsuleColliderRef.radius /* here you have access to collider radius. */
);
}
This code considers that your capsule is on Y-axis direction. You will get something like this:
Ps.: I know it’s an old question, but I decided to answers since I had this doubt and this is one of the first Google results.
The previous answer on this question seems to only work for vertical capsules. If you try it on arbitrarily rotated capsules, you’ll get positions.
I’ve been using the Physics Extensions in this project: UnityExtensions/PhysicsExtensions.cs at master · justonia/UnityExtensions · GitHub
It contains a “ToWorldSpaceCapsule” extension method for getting the start, end, and radius of a capsule collider, regardless of the capsule’s rotation.
The capsule used for the Primitive Type is just a regular mesh, there are no values for radius and length/height, except those which you would have to pull from the mesh’ vertex data.
Thanks for the clarification.
I calculated this is runtime. And after a while decided to use, colliders with triggers to get this done.
Thanks.