- Home /
Get capsule details (start, end, radius) from gameobject
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.
Answer by Chrisdbhr · Jul 21, 2019 at 11:37 PM
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.
Answer by dgoyette · Apr 05, 2020 at 02:40 PM
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: https://github.com/justonia/UnityExtensions/blob/master/PhysicsExtensions.cs
It contains a "ToWorldSpaceCapsule" extension method for getting the start, end, and radius of a capsule collider, regardless of the capsule's rotation.
Thanks for the pointer to this code. It's exactly what I was looking for.
Answer by Cherno · Jul 05, 2015 at 09:43 PM
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.
Answer by VaporAnomaly · Jul 10, 2015 at 08:35 AM
Thanks for the clarification. I calculated this is runtime. And after a while decided to use, colliders with triggers to get this done.
Thanks.
Your answer
Follow this Question
Related Questions
instantiating Transform insted of PrimativeType 1 Answer
Spawnning an object other than primitive makes the function run again and again 0 Answers
Capsule Cast Direction Help 1 Answer
Do Unity primitives incur performance penalties? If so, why? 0 Answers
Problem when manually creating game object in scene 3 Answers