Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 CiberX15 · Feb 14, 2014 at 06:08 AM · physics2d

Raycast2D hits itslf

Ok so I am probably just doing something wrong since the 2D physics is all new. But what I am trying to do is fairly simple. Run a raycast from a square with a Rigidbody2D and BoxColider2D attached. I am using the code:

 //Trace Down
 HitInfo = Physics2D.Raycast(transform.position, -transform.up, 1);
     if(HitInfo.collider != null)
     {
         Debug.Log("HIT " + HitInfo.collider);
         if(HitInfo.collider.GetComponent(Hull) != null)
         {
             NewLinks.Add(HitInfo.collider.GetComponent(Hull));
         }
     }

Problem is according to that log it is hitting itself. I can't use a mask because it is looking for other squares that are just like it so if it cant see itself it also would not be able to detect other like squares.

With a normal 3D raycast this wouldn't be a problem since the ray is originating inside the mesh faces and thus would not count the collision.

Any ideas?

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

2 Replies

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

Answer by robertbu · Feb 14, 2014 at 06:41 AM

A bit of a hack, but you can turn the collider off and back on for the current object around the Raycast2D:

 collider2D.enabled = false;
 HitInfo = Physics2D.Raycast(transform.position, -transform.up, 1);
 collider2D.enabled = true;
Comment
Add comment · Show 3 · 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 CiberX15 · Feb 14, 2014 at 05:14 PM 0
Share

That worked for me. Its kind of ugly though. It would be nice if the devs make ad an option or something so Raycast2D wont hit the object that they are being emitted from by default.

Again its less of a problem with 3D since collision is typically one sided, but 2D physics seem to work differently.

avatar image robertbu · Feb 14, 2014 at 06:18 PM 0
Share

A more traditional approach would be @whydoitoit's solutions. I did not suggest RaycastAll() and RaycastAllNonAlloc() because I though the results did not guarantee order (i.e. they should be considered unsorted) like the ones from Physics.RaycastAll(), and the extra overhead of sorting made the solution a bit ugly. But according to what I just read in the reference for Physics2D.RaycastAll():

The colliders in the array are sorted in order of distance from the origin point.

So this might be a better approach. If the returned array has more than 1 item, then the item at index 1 will be your first hit.

avatar image whydoidoit · Feb 15, 2014 at 08:19 AM 1
Share

Yeah and if you use the NonAlloc version you can pass a two element array, which is kind of nice.

avatar image
1

Answer by whydoidoit · Feb 14, 2014 at 07:04 AM

You could use RaycastAll or RaycastAllNonAlloc to get all of the collisions of the ray or you could offset the start of your ray by the size of the existing collider.

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

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

19 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

Related Questions

How Physics2D.OverlapCircleAll works??? 1 Answer

Drag rigid body based on touch in 2D 0 Answers

how polygon collider 2d calculates center of mass? 2 Answers

Game not working on Tizen 0 Answers

LayerMask help 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