Created
November 14, 2012 14:31
-
-
Save attilam/4072422 to your computer and use it in GitHub Desktop.
Raycast for the closest object with a specific tag in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool RaycastWithTag(Ray ray, out RaycastHit hit, float distance, string tag, int layerMask = System.Int32.MaxValue) { | |
RaycastHit[] hits = Physics.RaycastAll(ray, distance, layerMask); | |
hit = new RaycastHit(); | |
hit.distance = distance; | |
foreach(RaycastHit ahit in hits) { | |
if ( ahit.distance < hit.distance && ahit.transform.CompareTag(tag) ) { | |
hit = ahit; | |
} | |
} | |
if (hit.distance < distance) | |
return true; | |
else | |
return false; | |
} |
LaikWQC
commented
Apr 27, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment