- Home /
How to get correct scale for CircleCollider2D radius?
I working on a pool type 2D game using the new Unity 4.3 2D tools and it appears that I've run into a little ditch. To effectively make a shot, I need to locate a point on the circumference of the ball and target same. The code I use to do this is shown below;
Vector2 startPoint = ConvertTo2D(source.transform.position);
Vector2 targetPoint = ConvertTo2D(target.transform.position);
Vector2 lineOfSight = targetPoint - startPoint;
lineOfSight.Normalize();
if(source.collider2D is CircleCollider2D) {
startPoint = startPoint + lineOfSight * (CircleCollider2D)source.collider2D).radius;
}
The problem is that the dimension returned for radius from the circle collider is much larger than expected an it appears that this is based on the physics engine simulation and not world space dimensions. How can I easily convert dimensions from physics engine scale to world space?
Answer by mikembley · Jul 16, 2015 at 09:15 AM
The best way i have found is to use the largest localscale axis
float scaledRadius = Mathf.Max(transform.localscale.x, transform.localscale.y);
Answer by mgsteinkamp · Apr 22, 2014 at 02:14 AM
Not sure if this is what you're looking for, but the radius is relative to the object that it is attached to and should be preset to .5. To convert to world space you would multiply the game object's diameter by the collider's radius.
Answer by bcombes · Apr 22, 2014 at 04:34 PM
Thanks for the response, I eventually found another way around this that didn't involve physics calculations.
Answer by Zarkend · Apr 10, 2019 at 06:59 AM
I know this is an old question, but just answering for future reference.
Check the parent's Scale since it seems that the collider radius scale with it (pretty strange behaviour).
Your answer
Follow this Question
Related Questions
Joint2D Tutorials 1 Answer
Get Width of 2d Game Object 1 Answer
2D Physics and Jump-Through Platforms 2 Answers
Sliding in a 2D side scrolling game? 1 Answer