- Home /
Detect if an object in 3D space is within/touches a UI image
I have a square aim set to the middle of the screen, this is an UI image. Now I want to detect if enemies (visualized by pink boxes here) are within this aimbox but also, and this is very important, if only a part of the enemy is within the aimbox.
What I found so far is that I can't use (please correct me if I'm wrong):
Raycast from midscreen, because then you have to aim directly at the enemy.
Boxcast because it only creates the box at the target raycast point.
Raycast from the enemy to screen to detect object on screenpoint, because the ray is cast from mid of the enemy.
Spherecast, because it's not a box and thus unreliable.
Using a box object (with collider) with a huge length into the screen, because visually it will not retain the size of the aimbox image.
I have tought about using several raycasts from the screen into the world using start points at 3,6,9,12 o'clock of the aimbox image position as well as from the cornors and midpoint of the UI aimbox image. But this seem like a really unreliable solution.