Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by blehhhh · May 08, 2014 at 08:33 PM · 2d-gameplaycollisiondetection

How to set circle collision detectors more accurate

Hi,

I have a rectangle object, the player. I have a collection of cloned circle shaped objects.

When the player collides with a circle the game ends, see code below. The problem is that at certain angles the collision is inaccurate. The rectangle never touches the circle yet a collision is detected. If anyone could point me how to make it more accurate it would be much appreciated.

 void OnCollisionEnter2D(Collision2D col)
 {
     if (col.collider.name == "Point(Clone)") {
     
         Die ();

        }
     }
Comment
Add comment · Show 1
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 blehhhh · May 09, 2014 at 05:49 PM 0
Share

For my circles I am doing a transform.position to change the starting position to random and a transform.localscale to change the size randomly

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Invertex · May 08, 2014 at 08:34 PM

They should be pretty accurate... But if it's causing you issue, you could use OverlapCircle instead of a Collider, which will be fully consistent, only when something actually enters the circle.

http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.OverlapCircle.html

Comment
Add comment · Show 6 · Share
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 blehhhh · May 08, 2014 at 09:16 PM 0
Share

Thanks, Any idea how I would edit my code above to use overlap circle ins$$anonymous$$d.

Bear in $$anonymous$$d all circles are different sizes.

Thanks again

avatar image Invertex · May 08, 2014 at 10:20 PM 0
Share

It depends... OverlapArea would be more efficient, by only calling it on your player sprites, ins$$anonymous$$d of OverlapCircle on all your other sprites. Here's the simplest implementation for you:

 private Vector2 sBounds;
     private Collider2D hitCol;
     private Vector2 curPos;
 
     void Start ()
     {
         sBounds = GetComponent<SpriteRenderer>().bounds.extents;
     }
     
     void Update ()
     {
         curPos = transform.position;
         if( (hitCol = Physics2D.OverlapArea(curPos - sBounds,curPos + sBounds)) 
            && hitCol.name == "Point(Clone)")
         {
             Die();
         }
     }
avatar image blehhhh · May 09, 2014 at 05:20 PM 0
Share

Hi Invertex,

Thanks for the code above.

I have tried it, however it seems to be worse with this method than previously. Sometimes doesnt detect collision when there is one. I'm not sure if its something to do with my circular Textures. Its just a png I created with a transparent background. Its like as if the collision is detected on an invisible square around the circle and not the circle itself.

avatar image drudiverse · May 09, 2014 at 05:22 PM 0
Share

try changing collision detection, in collider settings. are you overriding any positions using transform position etc?

avatar image blehhhh · May 09, 2014 at 06:08 PM 0
Share

For my circles I am doing a transform.position to change the starting position to random and a transform.localscale to change the size randomly

Show more comments

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

22 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

Related Questions

How to check Hypothetical Collisions in 2D 1 Answer

How to get an AI move around in an asteroid field 0 Answers

General Unity 2D Character/Environment Control Questions 2 Answers

No changing Linear Drag (.linearDrag) on rigidbody / rigidbody2D in code? 1 Answer

Cloud recognition in Vuforia 0 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