- Home /
Is OverlapCircle broken?
public bool walled;
void Awake ()
{
// Reference the players transform and get the radius of the trigger.
player = GameObject.FindGameObjectWithTag("Player").transform;
CircleCollider2D[] colliders = player.transform.GetComponents<CircleCollider2D>();
foreach (CircleCollider2D collider in colliders)
{
if (collider.isTrigger)
{
jumpEnablerRadius = collider.radius;
}
}
}
void Update ()
{
walled = Physics2D.OverlapCircle (new Vector2 (player.position.x,player.position.y), jumpEnablerRadius,1 << LayerMask.NameToLayer ("Wall"));
}
I was under the impression that with this code the Radius at which the OverlapCircle checks is the same as the radius of a trigger collider. However this isn't the case when playtesting. So does this radius value get added to the player object, or does it begin from the center of the player object?
What is the scale of the object the collider is attached to? The radius of the collider is in local coordinates and must be scaled by localScale to find the world size.
Say your radius of the collider was .66666 and the local scale of your object was (.6, .6, 1). The size of the collider in world space would be:
.666666 * .6 = .4
As a test, put a circle collider on a Quad. A Quad has world size of (1,1,1) and the radius of the collider will be 0.5. Now double the size of the Quad (2,2,1). The circle has a radius twice as big, but the radius reported for the collider should sill be .5. The world size would be:
0.5 * 2.0 = 1.0
I'm sorry I'm having a hard time figuring this out. Are you saying that the radius parameter in OverlapCircle is multiplied by the radius of my trigger collider? Cuz OverlapCircle radius is a float is it not?
Physics2D.OverlapCircle (new Vector2 (player.position.x,player.position.y), jumpEnablerRadius,1 << Layer$$anonymous$$ask.NameToLayer ("Wall"));
This basically says that I want to draw circle with a center of my player object, with a 0.666f radius. So I can't understand why does the 0.666f radius of the OverlapCircle function is not equal to the 0.666f radius of my trigger collider. The function doesn't take any objects, just float values.
No matter if I pass in jumpEnablerRadius or just type a 0.666f, the results are the same.
As mentioned, you need to multiply your radius you pass to the OverlapCircle() by either the localScale.x or localScale.y of your player. Again:
.66666 * .6 = 0.4
So the code would like:
Physics2D.OverlapCircle (player.position, jumpEnablerRadius * player.localScale.x, 1 << Layer$$anonymous$$ask.NameToLayer ("Wall"));
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
[C#] "var" problem 3 Answers
Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers
Multiple Cars not working 1 Answer