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 Decinoge · Feb 05, 2015 at 12:51 PM · collider2draycasthit2dedge detectioncollider.raycast

How to make CircleCast work properly with multi vertex EdgeCollider2D

Hello everyone. I'm not really used to asking for help in here, so I'll try to keep it as tidy as I can.

[Context]

I am trying to make a ball bounce within a group of Colliders (using BoxCollider2D and EdgeCollider2D) that compose the level's limits. I wanted to use my own custom physics, without Unity's physics influence, for gameplay purposes. I have removed all the RigidBodies from the colliding Objects, and did the collision solving code myself resorting to Unity's Raycast functions. The code in question is the following:

 private List<HitInfo> check_collisions(Vector2 pos, Vector2 spd, out Vector2 new_speed)
 {
     float ray_length     = spd.magnitude;
     Vector2 dir         = spd.normalized;
     RaycastHit2D hit     = Physics2D.CircleCast(pos, radius, dir, ray_length, collision_mask);
     List<HitInfo> list    = new List<HitInfo>();
     HitInfo info        = new HitInfo();
     
     list.Add(info);
     
     if(hit.collider != null)
     {
         bool on_going;
         float bounced;
         float perm;
         
         on_going = true;
         bounced = 1;
         info.from_position     = pos;
         info.from_fraction    = 0;
         
         while(on_going)
         {
             perm = 2.0f * Vector2.Dot(dir, hit.normal);
             dir = dir - (hit.normal * perm);
             
             pos = (hit.point + (hit.normal * radius));
             ray_length *= (1 - hit.fraction) * bounce;
             bounced *= bounce;
             
             info.speed = pos - info.from_position;
             info.to_position = pos;
             info.to_fraction = hit.fraction;
             info = new HitInfo();
             list.Add(info);
             info.from_position = pos;
             info.from_fraction = hit.fraction;
             
             if(ray_length > 0)
             {
                 hit = Physics2D.CircleCast(pos, radius, dir, ray_length, collision_mask);
                 
                 if(hit.collider == null)
                 {
                     on_going = false;
                     
                     info.speed = (dir * ray_length);
                     info.to_position = pos + info.speed;
                     info.to_fraction = 1;
                 }
             }
             else
             {
                 on_going = false;
                 
                 info.speed = (dir * ray_length);
                 info.to_position = pos + info.speed;
                 info.to_fraction = 1;
             }
         }
         
         new_speed = dir * (speed.magnitude * bounced);
     }
     else
     {
         info.from_position = pos;
         info.to_position = pos + spd;
         info.speed = spd;
         info.from_fraction = 0;
         info.to_fraction = 1;
         
         new_speed = spd;
     }
     
     return list;
 }



[Problem]

The ball is able to collide quite perfectly with both BoxCollider2D and EdgeCollider2D with only 2 vertices. Sometimes however, it simply doesn't detect an EdgeCollider2D with various vertices. I even added RigidBodies to the Ball and the EdgeCollider2D producing the exact same results. I have created a few screenshots with different Ball speeds, and all of them with the a Kinematic RigidBody in each colliding object.

  • 1000 (pixels) speed units:Pic1 - Pic2 - Pic3

  • 100 (pixels) speed units: Pic4 The ball was already outside the collision because the condition for the player to pause was for the ball to be outside the screen.

  • 10000 (pixels) speed units: Pic5

As you can see, while it collides perfectly with both the EdgeCollider2D (with only 2 vertices) at the top of the screen, and the 2 BoxCollider2D at the bottom, every once in a while it fails to detect the EdgeCollider2D with multiple vertices on both sides (I only have pictures of the ball leaving the left side, but it does the same with the right). All this error happen only moments after I start the game at the higher speeds.

One detail though. This collisions are not "hand made" in the scene window. I have an inspector editor script that allow me to create this walls more easily.

More importantly, if I remove all the vertices of the EdgeCollider2D leaving only 2 (still using my inspector editor), the collisions work perfectly. As is can be seen on the following picture, even after over an hour play by itself, the ball keeps doing spaghetti: link text

Did anyone else found this kind of problem? Is there any way around it without having to use multiple EdgeCollider2D or multiple BoxColliders?

Comment
Add comment
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

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

2 People are following this question.

avatar image avatar image

Related Questions

Is there a way of casting Rays at the end of the collider of an object ? 0 Answers

How can i shoot 2D ball/bounce of wall using Raycast? 1 Answer

Is there a way to only acces one collider when there are many colliders with the same tag? 1 Answer

HOW TO ONLY DETECT ANGLES BY COLLIDERS2D FACES 0 Answers

How can I do a 2D raycast against one specific collider? 3 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