Measure the distance between the mouse click and the box.I am trying to do it using code Vector3.Distance.
![alt text][1]
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.
![alt text][2]
if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit))
{
if (hit.collider.transform.tag == "redbox")
{
Debug.DrawRay (mouseRay.origin, mouseRay.direction * 50, Color.red);
}
[1]: /storage/temp/91817-selected.png
[2]: /storage/temp/91945-drawray.png
↧