Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by camomatic · Jan 20, 2018 at 06:44 PM · raycastcolliderlayerslayermask

Why is Physics.Raycast returning colliders in a layer not included in the layermask?

I've been through several similar questions and I still cannot get my code to work, so I'm offering it up in the hope that someone can point out my mistake.


I have a city object that generates building objects. To do this it does a Raycast on objects in the "Planet" layer to find the location for the buildings and then checks there are no buildings already in that location by using a SphereCast in the "Buildings" layer. The city object has its own collider, but is marked as being in the "City" layer. However, instead of generating buildings on the Raycast contact with the planet, buildings are being generated on the city object, as if it is not being ignored by the raycast. All I can imagine is that I have misunderstood how the raycast method works and what it is returning. Can anyone point out a mistake?


In summary: Physics.Raycast is returning a collider in the "City" layer, even though I have specified the layermask as including "Planet" only.

The code:

 while (i < houses) {
     Vector2 rand = radius * Random.insideUnitCircle;
     Vector3 pos = transform.position + transform.TransformDirection (rand.x, rand.y, 100f);
 
     Ray ray = new Ray (pos, 110f * -pos);
     RaycastHit hit;
 
     //Draw ray to collide with planet layer. THIS IS THE PROBLEM LINE
     if (Physics.Raycast (ray, out hit, LayerMask.GetMask ("Planet"))) { 
         //...and not touching any other houses
         if (!Physics.CheckSphere (hit.point, 2f, LayerMask.GetMask ("Building"))) {
             //Spawn    
             Transform spawned = Object.Instantiate (housePrefab, hit.point, transform.rotation);
             spawned.Rotate (0f, 0f, Random.value * 360f);
             i++;
         }
     }
 
     n++;
     if (n > maxAttempts) {
         Debug.Log ("Time out error: Unable to place " + (houses - i) + " houses.");
         return;
     }
 }

Example of current wrong behaviour. (City object is red for illustration) alt text

Correct behaviour by turning off the city collider. alt text

captura-de-pantalla-2018-01-20-a-las-190206.png (460.7 kB)
captura-de-pantalla-2018-01-20-a-las-190241.png (446.1 kB)
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
3
Best Answer

Answer by Bunny83 · Jan 20, 2018 at 07:32 PM

You pass your mask as distance, not as mask. As you can see in the signature after the RaycastHit parameter comes the distance followed by the layermask.

 public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

If you want to do an infinite raycast you have to pass Mathf.Infinity (or float.PositiveInfinity) as third parameter and pass your layermask as fourth parameter.

 if (Physics.Raycast (ray, out hit, Mathf.Infinity, LayerMask.GetMask ("Planet"))) { 


Note that instead of casting a Physics.Raycast against your planet you can also use Collider.Raycast. This method will only cast against this one collider and will ignore anything else. It's actually more efficient to use Collider.Raycast if you only want to hit one specific 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 camomatic · Jan 21, 2018 at 06:59 AM 0
Share

Thanks for this! I wasn't aware of Collider.Raycast, but will be using that from now on

avatar image
0

Answer by camomatic · Jan 20, 2018 at 07:54 PM

Problem solved. I had entered the LayerMask in the Raycast distance parameter. The method should be Physics.Raycast (Ray, RaycastHit, maxDistance, layerMask). The offending line now reads:

 if (Physics.Raycast (ray, out hit, 100f, LayerMask.GetMask ("Planet"))) {
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

104 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

Related Questions

LayerMask for RayCast 1 Answer

Colliders attached to objects on separate layers are colliding? 1 Answer

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

How do I use layermasks? 9 Answers

How to create User layer that acts like the "Ignore Raycast" layer? 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