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
0
Question by Fire-Dragon-DoL · Dec 03, 2012 at 11:51 PM · raycastlayersray

Why Raycast hit a layer that I have removed from mask?

It sounds a bit strange but I made a mask which is `~LayerMask.NameToLayer("Tower")` which should "hit" everything except that layer:

     Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     bool raycastResult = Physics.Raycast(ray, out hit, Mathf.Infinity, ~LayerMask.NameToLayer("Tower"));
     if (raycastResult)
         Debug.Log("Layer: " + LayerMask.LayerToName(hit.transform.gameObject.layer));

However, Debug.Log outputs: "Layer: Tower". Am I doing something wrong?

I tried by reverting the layermask, and obviusly I can't hit anything (except towers which are not placed). The layer is correctly set on the tower.

Is it a bug maybe?

Edit 1: I still can't find a solution, are there any possibility that there is a "hidden" layer added to my prefab?

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

Answer by Bunny83 · Dec 04, 2012 at 06:21 PM

According to LayerMask.NameToLayer it does return the layer index and not it's layermask. So use it like this:

 ~(1<<LayerMask.NameToLayer("Tower"))

You just can test it by Debug.Log what ever the function returns. This would be the first thing i would check ;)

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 Fire-Dragon-DoL · Dec 04, 2012 at 06:26 PM 0
Share

Oh man, if that's the problem I wasted an entire day.

avatar image Bunny83 · Dec 04, 2012 at 06:37 PM 0
Share

Well, I admit that those two static functions are badly placed. They actually have nothing to do with the Layer$$anonymous$$ask itself. The Layer$$anonymous$$ask class is just a wrapper for an integer value. The class itself does no conversion at all. The only advantage is that Unity will display the Layermask dropdown in the inspector when you use this type on a public variable.

The Layername-to-index and index-to-layername works with the layer index which is stored in the layer property of a GameObject.

avatar image Fire-Dragon-DoL · Dec 04, 2012 at 06:39 PM 0
Share

Seems you are right, thanks a lot. Still I wasted 1 day <.<

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

10 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

Related Questions

When i bring my mouse on layer mask my player doesn't move 0 Answers

How do i add a layer to a gameobject? 1 Answer

Make Certain Object's Raycasts Ignore Certain Colliders 0 Answers

What is the best way to navigate through a grid based level using Raycasting ? 2 Answers

How could I combine melee weapons and rays? 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