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
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
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
}