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 Cyber_Defect · Apr 28, 2013 at 04:36 PM · layermaskdrawlinedrawray

LayerMask, DrawRay and DrawLine acting weird...

Ok, so I am getting some odd results when I use Physics.DrawRay().

I have created a function for detecting collisions with other game objects on Layer 10.

Unfortunately, while the ray passes through the object, it doesn't record a collision. (Yes I am certain the object has a Mesh Collider on the appropriate layer).

However, if I then use my LayerMask, none of the collisions happen, and it tells me the length must be greater than 0. (I am trying to ignore Layer 0).

In the image below, Red rays indicate no hit, Yellow means the collider is not hitting the appropriate object, and green means it is correct.

Here is a code snippet:

 GameObject MakeRayCasters(Vector3[] corners){
         GameObject origin = new GameObject("RayCaster");
         Vector3 average = new Vector3();
         for (int i = 0; i < corners.Length; i++) {
             average += corners[i];
         }
         average /= corners.Length;
         RaycastHit hit;
         Vector3 PlanarUp = Vector3.Cross((Vector3.zero - northPoints[1]).normalized,(Vector3.zero - northPoints[0]).normalized);
         LayerMask ignoreLayer = new LayerMask();
         ignoreLayer.value = 0;
         
         origin.transform.position = average;
         origin.transform.localRotation = Quaternion.LookRotation((Vector3.zero - origin.transform.position).normalized,PlanarUp);
         
         if (Physics.Raycast(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50, out hit)) {
             if (hit.collider.gameObject.layer==10) {
                 Debug.Log(hit.collider.gameObject.name);
                 Debug.DrawRay(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50,Color.green,Mathf.Infinity);
             } else {
                 Debug.Log("INVALID HIT: " + hit.collider.gameObject.name);
                 Debug.DrawRay(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50,Color.yellow,Mathf.Infinity);
             }
         } else {
             Debug.DrawRay(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50,Color.red,Mathf.Infinity);
             Debug.Log("MISS: " + hit.point);
         }
         RayCasters.Add(origin);
         return origin;
     }


Here is a screenshot:

alt text

drawrays.jpg (94.4 kB)
Comment
Add comment · Show 3
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 Cyber_Defect · Apr 28, 2013 at 05:14 PM 0
Share

$$anonymous$$aybe it's a collision angle thing?

avatar image Cyber_Defect · Apr 28, 2013 at 05:33 PM 0
Share

After further testing, it either has something to do with the angle, or the objects are not being rendered yet...

Which seems a little confusing... I have not setup any coroutines, so why would some objects not be rendered yet?

avatar image Cyber_Defect · Apr 28, 2013 at 05:36 PM 0
Share

The invalid hits are registering prematurely on other objects, that are not Instantiated until after the ground has been fully constructed. The Rays are cast before they are even Instantiated...

1 Reply

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

Answer by Cyber_Defect · Apr 28, 2013 at 05:43 PM

I think I figured out my problem, logic error. I am calling the ray as I build each floor. I need to call it after I build ALL floors :(

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

12 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

Related Questions

Add layermask to DrawLine & DrawRay or something similar with visible Ray 1 Answer

Draw a Ray to the Mouse Postion 1 Answer

DrawLine GUI confusion 0 Answers

Draw NavMesh path with multiple points set 0 Answers

Handles.DrawLine from transform.forward in 2D 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