To send Raycasting to four corners ?

Measure the distance between the mouse click and the box.I am trying to do it using code Vector3.Distance.

Hi, I click on the object on the grid with the mouse and I want you to show the boxes that can be clicked with the next mouse.I want to do, just be able to choose the boxes around. So white boxes will not be selected.I have read similar topics.But, I could not find the answers I wanted.

  • hit.transform.position.x +1
  • hit.transform.position.x -1
  • hit.transform.position.y +1
  • hit.transform.position.y -1

if (Physics.Raycast (ray, out hit)) {} using, when I click the box, show me around green boxs.I have read similar issues but haven’t found the answers I wanted.
If the label of the boxes around is “whitebox”, (hit.collider.gameObject.tag == “whitebox”) These boxes are turning into green color. If the label is a red box, these boxes have already been selected.

EDİT 1 - Update:

I wrote a code like the following, but it did not work.

        redbox = GameObject.FindGameObjectWithTag("redbox").transform;
        
        Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        distance = Vector3.Distance (redbox.position, mousepos);

        if(Input.GetMouseButtonDown(0)) 
        {
        if(distance < 1.0f){print("Near the box");}
        if(distance > 1.0f){print("Away from the box");}
        }

EDİT 2 - Update:

When I looked at the box library, I compared it with the SphereCast code. It did not work the way I wanted when I wrote it like this.

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit sphereHit;
if (Input.GetMouseButtonDown (0)) 
{
if (Physics.SphereCast (ray, 1, out sphereHit)) 
{
if (sphereHit.collider.gameObject.tag == "redbox") { print ("Near the box"); }
if (sphereHit.collider.gameObject.tag == "whitebox") { print ("Away from the box"); }
}
}

EDİT 3 - Update:

  • I’m still investigating.I’m trying new methods because the answer is notcoming.Now I am looking at whether there is an object with a “redbox” tag around it by sending a raycasting.
  • The raycasting is going straight.Okay, but I want you to send a raycasting to the four corners when you get hit.
  • The angle at which the red raycasting comes is not fixed.Whatever the angle, the green raycastings must go in the same direction.

if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit))
{
if (hit.collider.transform.tag == "redbox") 
{
Debug.DrawRay (mouseRay.origin, mouseRay.direction * 50, Color.red);
}

hi;
i did similar thing in my game its not like your way but i described it here so maybe usefull;

this show’s me where i can go with red triangles;

Image 1

what i did was creating 3 GameObjects with smaller triangle form collider on them like this :

Image 2

then i create a script and attach to those colliders to pass their objects to their parent with the first trigger , the parent got all those objects and based on their name put them in an special array , when the parent full the array and he already know all of the triangls around him i created a function to destroy the colliders around him cause they are not any more needed;

so this is a tricky way and optimized to do it;