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 BoomSplockets · Aug 07, 2013 at 10:05 PM · raycastcollidercollidersraycastinglayermask

RaycastHit returns object without collider, in wrong layer

I have a Game Object with a script that has a function which raycasts four times around it to assess the Objects around it (Essentially, I have box colliders set up in a grid that check each other in a function). While these raycasts usually go through fine, there is a problem where the raycast keeps picking up certain objects (Walls, as planes in between the colliders) which have mesh collider turned off, and the raycast is set to pick up only layer 10, while these walls are on layer 12. Why is this happening, and how do I fix it?

With seemingly irrelated code removed, this is what I'm doing. print(colliderMapTile) returns an instantiated Prefab with it's collider turned off with it's layer set to 12.

 var layermask = 1 << 10;
 
 function updateSurroundingRooms(){
     var    cardinalDirection = new Vector3[4];
     cardinalDirection[0] = transform.TransformDirection (Vector3.forward);
     cardinalDirection[1] = transform.TransformDirection (Vector3.right);
     cardinalDirection[2] = transform.TransformDirection (Vector3.back);
     cardinalDirection[3] = transform.TransformDirection (Vector3.left);
     for(var i : int = 0; i < 4; i++){
         var hit : RaycastHit;
         var colliderMapTile : GameObject;
         if (Physics.Raycast (transform.position, cardinalDirection[i], hit, layermask)) {
             colliderMapTile = hit.collider.gameObject;
             print(colliderMapTile);
             colliderMapTile.GetComponent("MapTileColliderBehaviour").roomChange();
         }
     }
 }
Comment
Add comment · Show 4
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 robertbu · Aug 07, 2013 at 11:36 PM 0
Share

Not sure this has a impact on your issue, but layers are numbered from 0. So 1 << 10 will find the 11th layer in your list.

avatar image BoomSplockets · Aug 08, 2013 at 05:37 AM 0
Share

I'm pretty sure 1 << 10 will enable the 10th layer, and all the documentation I've read supports that. But without any other choice, I tried that. It didn't break what was already working. It changed absolutely nothing. I don't think my raycast is even using the Layer$$anonymous$$ask.

avatar image BoomSplockets · Aug 08, 2013 at 05:44 AM 0
Share

Ha. It isn't. I formatted the raycast wrong, and forgot to slip a '1' in before the layer mask for the raycast range. Actually, now that it isn't returning the wall objects, I tried playing with the layermask. It still works regardless of the value of the layermask, and I still wonder why the raycast was picking up an object without a collider.

avatar image robertbu · Aug 08, 2013 at 06:30 AM 0
Share

Layers are are zero based, If you pull down the Layer dropdown and select 'Edit layers', you will see "User Layer 10." That is the layer that 1 << 10 enables. But the layers start with "Element 0," so if you count, that is the 11th layer. If you do 1 << 0 (or just 1), you get the layer called "Element 0";

1 Reply

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

Answer by robhuhn · Aug 08, 2013 at 06:31 AM

There is no overload for

 Physics.Raycast (origin: Vector3, direction: Vector3, hitInfo: RaycastHit, layerMask: int)

In your code it takes the bitmask as the distance parameter.

static function Raycast(origin: Vector3, direction: Vector3, hitInfo: RaycastHit, distance: float, layerMask: int): bool;

Replace it with

 Physics.Raycast (transform.position, cardinalDirection[i], hit, yourDistance, layermask)


@edit

btw. if you use var mask : LayerMask on a component you should see the layermask dropdown in the inspector to configure layermasks easily. Anyway that's how it's done in c# public LayerMask mask and I guess it also works in us/js.

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 BoomSplockets · Aug 09, 2013 at 05:47 PM 0
Share

Right, that fixed it. Thanks

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

15 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

Related Questions

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

Even-odd test with raycasting 1 Answer

OnCollisionEnter2D is not working 0 Answers

raycasts only working with IsTrigger colliders? 1 Answer

SInce when?!?!? (Raycast issue) 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