Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MNNoxMortem · Nov 02, 2017 at 10:31 AM · collidermathcapsulecollider

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. Parent rotation 45°

When I set the parent rotation to 0 the drawn spheres align perfectly.

alt text

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?

parent-rotation-0.png (124.2 kB)
parent-rotation.png (144.6 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Suddoha · Nov 02, 2017 at 11:44 AM 0
Share

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.

avatar image MNNoxMortem Suddoha · Nov 03, 2017 at 10:04 AM 0
Share

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).

avatar image MNNoxMortem MNNoxMortem · Nov 03, 2017 at 11:50 AM 1
Share

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!

avatar image Helical · Sep 21, 2018 at 09:34 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Capsule collider going through terrain collider 1 Answer

Retrieve 2d primitives from 3d colliders 1 Answer

How do I calculate how far along a point is in relation to a line, when the point is not on the line? 1 Answer

Make sphere collider the center of gameobjects/Sum of vectors 3 Answers

Capsule collider bounces where two ground colliders meet 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges