• 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
0
Question by Kensei · Aug 08, 2014 at 02:39 PM · c#bugphysics2d

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?

alt text

Comment
Add comment · Show 10
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 robertbu · Aug 08, 2014 at 02:51 PM 0
Share

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.

avatar image Kensei · Aug 08, 2014 at 02:54 PM 0
Share
  1. on x and y, What do you mean scale in localscale?

avatar image robertbu · Aug 08, 2014 at 03:23 PM 0
Share

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  

avatar image Kensei · Aug 08, 2014 at 04:00 PM 0
Share

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.

avatar image robertbu · Aug 08, 2014 at 05:57 PM 1
Share

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"));
Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

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

MonoDevelop 4.0.1 code completion doesn't work 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