Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by isteffy · Apr 17, 2016 at 09:54 AM · raycastraydrawlinedrawray

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

I want to test my Ray but like all good rays, I need a layermask. Unfortunately, Unity does not add this option for their debugs for RayCast, DrawLine & DrawRay.

How can I find a work around for this?

I want to also be able to SEE my ray

Comment
Add comment · Show 2
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 isteffy · Apr 17, 2016 at 12:52 PM 0
Share

Why does this question get downvoted? I think the lack of including the layermask in DrawLine & DrawRay is a huge problem for a lot of people who want to accurately test whether their Ray is hitting its designated target and if not, where it wrong.

avatar image andrew-lukasik isteffy · Apr 17, 2016 at 08:52 PM 1
Share

DrawLine does not need a layermask since it does not not perform any physics raycast call at all. Both DrawLine & DrawRay only draws a simple line. So, you need to perform a Physics.Raycast and then feed the results to Debug functions appropriately.

Downvote was for asking wrong question ;)

1 Reply

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

Answer by andrew-lukasik · Apr 17, 2016 at 09:41 PM

EDIT: Newer and little more advanced method for programmers watching their GC stats: | debug visible raycasts | no GC Raycasts | no garbage raycasts |

Step A: Create class ExtensionMethods.cs and/or Cast method in it:

 using UnityEngine;
 
 public static class ExtensionMethods {
     
     public static bool Cast ( this Ray thisRay , out RaycastHit hit , float range , int layermask ) {
         if( Physics.Raycast( thisRay , out hit , range , layermask ) ) {
             #if UNITY_EDITOR
             Debug.DrawLine( thisRay.origin , hit.point , Color.green , 1f );
             #endif
             return true;
         }
         else {
             #if UNITY_EDITOR
             Debug.DrawRay( thisRay.origin , thisRay.direction*range , Color.white , 1f );
             #endif
             return false;
         }
     }
 
 }

Step B: Always see your raycasts from now on and be merry:

 public class AnyClass : MonoBehaviour {
     
     void Update () {
         RaycastHit hit;
         Ray ray = new Ray( transform.position, transform.forward );
         ray.Cast( out hit , 10f , 1<<0 );
     }
 
 }

btw. since Cast method returns bool you can put it in if/else statement just like with Physics.Raycast calls before:

 if( ray.Cast( out hit , 10f , 1<<0 ) ) {
     //hit
 }
 else {
     //no hit
 }


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 isteffy · Apr 18, 2016 at 06:31 AM 0
Share

Thanks for help :) There is an error though. 'ray.origin = transform.position;' is throwing compiler error:
error CS0165: Use of unassigned local variable `ray'

Any idea why? I thought Ray ray would be assigning ray. Also 'Cast' in ray.Cast is highlighted red in $$anonymous$$onoDevelop-Unity although this could be due to local variable 'ray' not being assigned.

avatar image andrew-lukasik isteffy · Apr 18, 2016 at 09:42 AM 1
Share

Ah ok, it's easy to fix once you know meaning behind "unassigned exception". It just means that we're trying to use variable "ray" without assigning anything to it first. To fix that you need to replace variable creating line (meaning "Ray ray;") with

"Ray ray = new Ray();"

or

Ray ray = new Ray( transform.position, transform.forward );

avatar image isteffy andrew-lukasik · May 02, 2016 at 09:38 AM 0
Share

This works like magic. Thank you, oh great unity wizard!

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

53 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast won't hit Collider 1 Answer

Problem With Raycast Pickup Using Touch 0 Answers

camera.ScreenPointToRay always has same origin... 1 Answer

How can I can I cast a ray from a gameobject? 1 Answer

Raytracing through an octree? 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