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
1
Question by SuperMasterBlasterLaser · Nov 08, 2014 at 11:06 AM · raycastcollidercollidersraycasthit2d

RaycastHit2D hits "itself"

I have gameobjects with circle colliders, and walls with box colliders. I make raycast to detect if there is something in front:

   RaycastHit2D hit = Physics2D.Raycast(transform.position, currentNode.position);

However when I write the name of gameobject which raycast is hit, it shows me that it hit itself. Do I need to disable my Collider and then enable it after raycast? I know that there is masking, but I can't use it, because other gameobjects will raycast too to detect this gameobject. 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

3 Replies

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

Answer by 767_2 · Nov 08, 2014 at 01:01 PM

you should put yourself on a layer ,make a new layer for example layer 8 and assign it to your object and then tell the raycast to do its job on all layers except your player layer, like below

  int layerMask = 1 << 8;
         
     // This would cast rays only against colliders in layer 8.
     // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
     layerMask = ~layerMask;
     RaycastHit hit;
     // Does the ray intersect any objects excluding the player layer
     if (Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), out hit, Mathf.Infinity, layerMask)) 

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
1

Answer by Tsilliev · Mar 19, 2018 at 12:29 PM

My solution was to turn off the collider before the raycast and turn it on at the end:

 for (int i = 0; i < sectorCount; i++) 
 {
 locations [i].GetComponent<CircleCollider2D>().enabled = false;
                 
 RaycastHit2D hit = Physics2D.Raycast (locations [i].transform.position, Vector2.up);
 .
 .
 stuff
 .
 .
 locations [i].GetComponent<CircleCollider2D>().enabled = true;
 }
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 amiel_ace · Nov 08, 2014 at 11:15 AM

Try using a Trigger Collider.

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 SuperMasterBlasterLaser · Nov 08, 2014 at 11:33 AM 0
Share

Still hits itself.

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

29 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

Related Questions

OnCollisionEnter2D is not working 0 Answers

raycasts only working with IsTrigger colliders? 1 Answer

What drawback of frequently changing of Game Object's Layer? 0 Answers

Raycast 2D hits sprite with collider, but returns error when not hitting any collider 2 Answers

How can I let the ray to the edge of the capsule? 1 Answer


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