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
4
Question by Suyuanhan · Oct 11, 2011 at 03:49 PM · physicsraycastlayermask

Physics Raycast LayerMask problem?

Here's the code

  public LayerMask lm;//I have already set this "cube" in Inspector
 
 void Start () {
 
 }
 
 
 void Update () {
     RaycastHit rayHit;
     if (!Physics.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition),100, out rayHit, lm))
         return;

     Debug.DrawLine(Camera.mainCamera.ScreenPointToRay(Input.mousePosition).origin, rayHit.point);
 }

I have set two different layer: "cube" and "sphere",and I put two objects which change their layer one is "cube",the other is "sphere".When I test in Game , I found that both Objects can be hit by the ray.

So,is there something I set wrong?How can I just set it just hit the layer of "cube"?

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
25
Best Answer

Answer by Suyuanhan · Oct 12, 2011 at 03:45 AM

I found the problem: 1. The Raycast function have to need a parameter which is "distance" , or it won't select the layermask; 2. The "distance" parameter have to be at the place before "layermask" parameter,like: if (!Physics.Raycast(ray, out rayHit, 100, lm)) but if you set the wrong place like the code in my question,there is nothing wrong to Unity3d,but it just can't select the layermask.

Is it a bug?

ps:it just fine when you use layermask or layermask.value

Comment
Add comment · Show 2 · 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 kromenak · Oct 12, 2011 at 05:34 PM 0
Share

Yeah, at second glance it appears that the arguments you passed in to Raycast don't match any of the function definitions exactly; you had the ray and distance arguments swapped.

The difference for me is that the raycast call in your original post does return an error when I try to compile it, saying that it doesn't match any definition of Raycast.

avatar image insominx · Jun 12, 2012 at 04:05 PM 0
Share

Right, the example does fit any existing function prototypes. One thing to watch for here is automatic type conversion from int (your layermask) to float (the distance). The compiler will happily accept something like Physics.Raycast(ray, out hit,~Layer$$anonymous$$ask.NameToLayer("$$anonymous$$yLayerName")) where the layer mask here gets used as the distance.

avatar image
2

Answer by kromenak · Oct 11, 2011 at 06:57 PM

The Physics.Raycast function does not take a LayerMask variable, it takes an int variable. You can convert the LayerMask value to an integer value like this:

 lm.value

As mentioned here: http://unity3d.com/support/documentation/ScriptReference/LayerMask.html

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 JINJITOR · Jan 22, 2019 at 10:11 PM

thank u bro, you foda

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Four different Raycast functions? 1 Answer

Raycast not working 2 Answers

2D Raycast Layer Mask not working 1 Answer

Physics2D, weird layerMask behavior on Linecast, Raycast and OverlapPoint 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