Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by FakePL · Mar 03, 2019 at 03:10 PM · 2dcollisioncast

Collider2D.Cast precision

I have a question about Collider2D.Cast function, it seems like while working with small numbers it returns collisions even if there shouldn't be any. Here is a picture, and some code: alt text

 using UnityEngine;
 
 public class test : MonoBehaviour
 {
     BoxCollider2D boxCollider;
 
     void Awake()
     {
         boxCollider = GetComponent<BoxCollider2D>();
     }
 
     void FixedUpdate()
     {
         RaycastHit2D[] hits = new RaycastHit2D[10];
         boxCollider.Cast(Vector2.up, hits, 2f);
         foreach(RaycastHit2D hit in hits)
         {
             if (!hit) break;
             Debug.DrawRay(transform.position, hit.point - hit.centroid, Color.red);
             Debug.DrawLine(hit.centroid, hit.point, Color.yellow);
         }
 
     }
 }

As you can see, |x| distance between two objects is 0.006, but collision is still detected. Is there something like Collider thickness? Also why hit.point - hit.centroid gives point outside of collider? I wanted to check where collision happened on my collider (left, right, up, down), but around the corners its inconsistent (looks like instead i should check for the same but not on a player collider but rather box that was hit, since it looks more precise and then react to that).

example.png (108.1 kB)
Comment
Add comment · Show 2
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 RobAnthem · Mar 03, 2019 at 03:24 PM 0
Share

TLDR: I could be wrong about all of this, and it may be that Unity just has issues with Physics and smaller objects.... but...


I believe it's because you're boxcasting, and the box is counted from center out, so it moves from center out, so half the extents of the box will find a collider before the center does. IE if you're boxcast is an extent of .0006f then it will collide at .0003f from center of the box. So are you sure box casting is what you want? Basically your centroid is showing outside the original box because it doesn't cast inside itself, so it moves half the extents out and begins casting.

avatar image FakePL · Mar 03, 2019 at 03:43 PM 0
Share

@RobAnthem .006 distance is between edges of colliders, not their origins (centroids). Actually i think maybe its because Unity uses calculations 1 unit = 100 pixels so 1 pixel = 0.01 unit ? So precision would be capable of doing precise calculations up to 2 digits after the dot and above that it uses rounding?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by FakePL · Mar 03, 2019 at 09:27 PM

I think i found an answer. There is something like: Physics.defaultContactOffset Default value is 0,01 and you can change it in project settings.

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

Answer by Bunny83 · Mar 03, 2019 at 04:13 PM

You actually use this method wrongly in your sample code. You completely ignore the return value of the method. This is extremely important. The array that you pass in has 10 pre allocated results. If the method returns a value of 0, none of those 10 results should be processed. The return value tells you how many results the method has been written into the results array. So you can not use a foreach loop here.


The general usecase would look like this:

 RaycastHit2D[] hits = new RaycastHit2D[10];
 void FixedUpdate()
 {
     int hitCount = boxCollider.Cast(Vector2.up, hits, 2f);
     for(int i = 0; i < hitCount; i++)
     {
         RaycastHit2D[] hit = hits[i] ;
         Debug.DrawRay(transform.position, hit.point - hit.centroid, Color.red);
         Debug.DrawLine(hit.centroid, hit.point, Color.yellow);
     }
 }

Note that i've declared and created the "hits" array outside the FixedUpdate method. The point of using a pre-allocated array is to avoid constant creation and garbage collection of the array.

Comment
Add comment · Show 1 · 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 FakePL · Mar 03, 2019 at 05:05 PM 1
Share

It's actually not wrong, there is line if (!hit) break; although I agree I can easily avoid creating new array each Update (and i should) - this answer doesn't provide anything to original question since the only difference is garbage collection.

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

238 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 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 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 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 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 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 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 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 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 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 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

Precise Collision Detection with Rigidbody2D.Cast 1 Answer

[c#]collision script not working 1 Answer

SetActive messes up everything? 1 Answer

Want to use iTween for 2d floor collision. 1 Answer

Need help fixing 2D Top-Down controls and collisions 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