- Home /
How Do I find the closest tagged object within a distance of a raycast.
I am trying to implement a soft target type ranged attack where I find the "best" target which is defined by: proximity to a "raycast" (not necessarily a ray cast but a straight line going from middle of the player's screen) and of those within x distance of the ray cast which are closest to the player. I've seen a few questions for finding all tagged objects within a certain distance but I don't want to search the whole scene, just a specified area.One thing I've considered is having the raycast have trigger colliders attached and detect it that way, but it seems overly complicated and I'm hoping there is an easier, more well documented way of doing this.
Answer by Lawlets · Oct 25, 2019 at 10:50 AM
If the tagged object have that you search have collider, you can use Physics.OverlapSphere / Physics.OverlapCapsule / Physics.OverlapBox and look on all collided object if one of them has the tagg :) I recommand you to check Unity.Physics. You should find some function that could help you.
After attempting this I ran into a problem which is the box that's created from these functions are always perpendicular to the global grid coordinates and because they don't rotate the "acceptable area for targets" is not consistent. Is there some way to ensure the box rotates appropriately with the player?
EDIT: after reviewing Unity.Physics I'm going to try and use Physics.BoxCast.
Your answer
Follow this Question
Related Questions
how detect "OnRaycastOut" 1 Answer
RaycastHit2D.collider always null? 0 Answers
Why does my Raycast stop detecting colliders 2 Answers
Can physic2d linecast and overlapcircle do a layer priority? 0 Answers
ricochet bounce effect issue 1 Answer