- Home /
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
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.
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 ;)
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
}
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.
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 );
This works like magic. Thank you, oh great unity wizard!
Your answer

Follow this Question
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