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 BBrown4 · Feb 13, 2013 at 02:20 AM · raycastlayershit

Raycast hitting the incorrect layer

Ok so I have to imagine that this is a problem with my code. I'm fairly new to raycasts, but the research I have done and the experiments I've done have shown me that Raycasts really aren't all that hard to use. Well, maybe it sensed me thinking that and decided to give me a hard time. Here is the screenshot,

alt text

Now here is my code (Explanation of problem is down below):

 using UnityEngine;
 using System.Collections;
 
 public class UnitSelectionTest : MonoBehaviour {
     
     RaycastHit hit;
     public static LayerMask terrainLayerMask = 1 << 8;
     public static LayerMask unitLayerMask = 1 << 10;
     
     
     // Update is called once per frame
     void Update () {
         if(Input.GetMouseButtonDown(0)) {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if(Physics.Raycast (ray, out hit, Mathf.Infinity, terrainLayerMask)) {
                 if(hit.collider.tag == "Terrain") {
                     print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
                     Debug.DrawLine(ray.origin, hit.point, Color.red, 60.0f);
                 }
             }
             
             if(Physics.Raycast (ray, out hit, Mathf.Infinity, unitLayerMask)) {
                 if(hit.collider.tag == "Selectable") {
                     print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
                     Debug.DrawLine(ray.origin, hit.point, Color.red, 60.0f);
                 }
             }
         }
     }
 }

Ok, so here's the problem. The above screenshot is showcasing, for good measure, a series of Raycasts being shot on all sides of the cube, minus the bottom. The raycasts are all detecting that they are hitting the terrain layer instead of the layer that the cube is in. Now I'm about 80% certain that this is an error with my code. But get this, when I move the cube high enough so that the terrain is no longer being viewed, the ray cast detects that it is hitting the cube. But as soon as I put it back down, it thinks that it's hitting the terrain again. So what do you recommend I do? I've done a lot of searching for a problem such as this but have found none that yielded working results. Is it my syntax? Do I have a block of code incorrectly placed? Why is it hitting the terrain when it's visible, but as soon as the terrain is taken out of the picture, or I completely comment out the "if-statement" checking to see if the raycast hit the terrain, is it working properly? My brain is fried and I'm out of ideas. Any and all help is very much appreciated.

unity 2013-02-12 17-27-13-08.png (324.5 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
0
Best Answer

Answer by BBrown4 · Feb 13, 2013 at 06:21 AM

Ok, well I seem to have fixed it.

     void Update () {
         if(Input.GetMouseButtonDown(0)) {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if(Physics.Raycast (ray, out hit, Mathf.Infinity, unitLayerMask)) {
                 if(hit.collider.tag == "Selectable") {
                     print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
                     Debug.DrawLine(ray.origin, hit.point, Color.red, 10.0f);
                 }
             } 
             
             else if(Physics.Raycast (ray, out hit, Mathf.Infinity, terrainLayerMask)) {
                 if(hit.collider.tag == "Terrain") {
                     print ("The raycast has hit " + hit.transform.gameObject.name + ", layer is " + hit.transform.gameObject.layer);
                     Debug.DrawLine(ray.origin, hit.point, Color.red, 10.0f);
                 }
             }
         }
     }

I guess it just takes a little playing around with. I figured maybe it was checking for two different raycasts in the same frame. So I probably should have done this from the start, but I just added the "else-if" statement instead of the two "if" statements so that way it would say, "Ok if it's not one then it must be the other". Makes sense to me, not sure if I made sense to anyone else haha but thanks for the help! :)

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 BBrown4 · Feb 13, 2013 at 05:47 AM 0
Share

I also changed the the layermasks as well.

 public static Layer$$anonymous$$ask terrainLayer$$anonymous$$ask = 1 << 8;
 public static Layer$$anonymous$$ask unitLayer$$anonymous$$ask = ~terrainLayer$$anonymous$$ask;
avatar image
0

Answer by iwaldrop · Feb 13, 2013 at 02:44 AM

I assume you mean to be checking the unitLayerMask in your second Raycast? Just to make sure your raycasting is working correctly, you can remove the layermask entirely. There is a cool Layer for objects that you don't want to include in raycasting called "IgnoreRaycast". Find it in the inspector.

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 iwaldrop · Feb 13, 2013 at 02:46 AM 0
Share

Another element to this is that you probably don't need to do a separate raycast for each layer you want to hit. Just do one raycast, ignore some layers via the mask if you want, and query the RaycastHit for what it hit. You're definitely on the right track!

avatar image BBrown4 · Feb 13, 2013 at 04:53 AM 0
Share

Right, I'm trying to get the raycast to ignore the terrain layer mask. The reason for the multiple casts is just to show that at no matter what angle they come from, they still seem to be hitting the terrain layer. For some reason, when the camera is looking at the terrain, the raycasts will always hit the terrain for some reason, but when the cube is high enough to where the camera is not pointing at the terrain, it hit detects that it is hitting the cube. Strange, I wonder if this has to do with what the camera is looking at, although, the ray variable is set to fire the raycast on the mouse position, shouldn't that be enough? Oh and I just realized I had a type in my code. In the second if statement it's supposed to look for the unitLayer$$anonymous$$ask not the terrainLayer$$anonymous$$ask again. I fixed it. It's the same issue but just wanted to let you know that's not the cause.

avatar image BBrown4 · Feb 13, 2013 at 05:27 AM 0
Share

Update: Well, I have been playing around with it and now I've gotten it to properly hit the cube, but the only issue now is that the raycast is going THROUGH the cube. So essentially it's hitting both. Is there a way to get the raycast to not go through the cube? Because if the raycast hits the cube, but goes through and hits the terrain also it would cause complications in my code.

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

Mesh collider as bullet hit detection performance 2 Answers

The Child Is Not Hovered When Passing The Parent's Collider 0 Answers

Ignoring an object (using raycast with layers) is not working 1 Answer

[2D] Raycast to mouse cursor = offset on Y axis 0 Answers

Raycast still passes through even with layers! 0 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