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
2
Question by PrimeDerektive · Jan 25, 2012 at 01:45 PM · raycastlayermaskspherecast

SphereCast doesn't work with a layerMask

Or at least, as far as I can tell it doesn't. To test, I was doing a Raycast and a SphereCast in the same frame with exactly the same parameters (except the obvious addition of the range parameter to SphereCast), both with a layerMask var I set up as a public member variable and set in the editor. The SphereCast would fail even when the Raycast evaluated to true.

Try this code, and make sure you place a large object with a collider (like a cube or something) in front of the object you attach this to.

 public var layerMask : LayerMask; //set this to something in the editor
 public var sphereRadius : float = 10.0;
 
 function Update(){
     
     var hit : RaycastHit;
 
     if(Physics.Raycast(transform.position, transform.forward, hit, 1000, layerMask)){
         Print("Raycast succeeded.");
     }
     else{
         Print("Raycast failed.");
     }
     
     if(Physics.SphereCast(transform.position, sphereRadius, transform.forward, hit, 1000, layerMask){
         Print("SphereCast succeeded.");
     }
     else{
         Print("SphereCast failed.");
     }

 }

Or am I doing something wrong?

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

1 Reply

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

Answer by Bunny83 · Jan 25, 2012 at 02:52 PM

Keep in mind that if the sphere starts inside or partial inside the object you want to hit it won't detect an hit. A radius of 10 seems quite big (i don't know the object relative scales you use). What exactly do you want to detect?

btw. i guess the second Raycast should be a SphereCast ;)

In general keep in mind that MeshColliders can only be hit from one side (outside). Convex MeshCollider or any Primitive colliders are volume-colliders (can't have holes or single planes / trinagles). If a Raycast starts from the inside it does never hit the object.

edit
I've just realised you've used the LayerMask utility-class, but the wrong way ;)

The layermask is a bit-flag (32-bit integer value) where each bit represents one of the 32 layers. You have to use layerMask.value in your Ray- / Spherecast.

Comment
Add comment · Show 8 · 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 PrimeDerektive · Jan 25, 2012 at 03:01 PM 0
Share

Ah right, sorry. Fixed it to be a SphereCast as it should be. I'm casting from the camera to objects with capsule colliders in my actual game, which are 2 units tall. I've tried many different radii.

The fact remains, it works if I remove the layer$$anonymous$$ask parameter from the SphereCast call (and even check the layer of the hit object ins$$anonymous$$d, inside the SphereCast evaluation), so the radius is irrelevant. However it will always fail if the layer$$anonymous$$ask is used in the SphereCast call, even if the Raycast succeeded. Bug?

avatar image Bunny83 · Jan 25, 2012 at 05:46 PM 0
Share

I've edited my answer ;)

avatar image PrimeDerektive · Jan 25, 2012 at 08:06 PM 0
Share

Hrm... that isn't true, at least for Raycasts. See Eric5h5's answer here (the last one): http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html

I've used it like that for almost 2 years now with Raycast and it absolutely works as intended. I'm guessing it just doesn't work with SphereCast for some reason.

I would say I'll try the .value method, but when you declare a public Layer$$anonymous$$ask member variable it creates a dropdown in the editor from which you could select multiple layers to be used in the mask. How would .value work in instances where I'm using a Layer$$anonymous$$ask with multiple layers?

avatar image PrimeDerektive · Jan 26, 2012 at 03:53 AM 1
Share

Well regardless, layer$$anonymous$$ask.value works with SphereCast :) so I guess I'll just use that. but I will continue to use just layer$$anonymous$$ask in my raycasts out of spite, because it works dammit! :)

avatar image Bunny83 · Jan 26, 2012 at 04:56 AM 0
Share

Yes, they implemented two implicit operators for Layer$$anonymous$$ask to convert a Layer$$anonymous$$ask to int and an int to Layer$$anonymous$$ask. I'm not sure why it shouldn't work with the implicit operator, but if something doesn't work the way you have it, you should stick to the documentation

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Trigonometry for Collision Detection 0 Answers

Raycasting with LayerMask gives me a bce0023 1 Answer

Layermask doesn't seem to work 2 Answers

Problem in spherecast . 1 Answer

Layermask (raycast) wont work... 4 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