# Calculate Mesh top, bottom, left and right position relative to mouse click position on the mesh

I have a mesh (suppose it can be circle, or rectangular,square or an arc). The geometry of the mesh can be anything. I can click on mesh at any position. From relative to the clicked position, I want to get its right, left top and bottom position of the mesh. Maybe the picture explain it better then my words

Update: I tried to apply a quick dirty solution. It is incomplete:

// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//Ray r = Ray(Input.mousePosition);
RaycastHit rayHit;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);

if(Physics.Raycast(r,out rayHit,1000))
{
Debug.Log(rayHit.transform.name);
Debug.Log(rayHit.point);
Debug.Log(rayHit.normal);
StartCoroutine(MoveToDirection(rayHit.point));
}
}

}

IEnumerator MoveToDirection(Vector3 startPosition)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = startPosition;
while (true)
{
go.transform.position=  go.transform.position + go.transform.up * Time.deltaTime* speed;
yield return new WaitForEndOfFrame();
}

}


On click i instantiated a gameobject and start moving in upward direction and when it leaves the mesh bounds i will get the position but it have differet problems, like: 1. It will not work with arc object or maybe with non rectangular geomerty. 2. I have to apply onTrigger and onExitEvent that can be skip when the cube move with fast pace.

Two possibilities here.

1. Average but fast solution: You could use the bounds of the mesh. Knowing the center and size of the bounds you could easily compare them to the position of the mouse and determine the left/right/top/bottom values that you need.

2. Precise but slower solution: As you know the mouse position you could project a line in left/right/top/bottom direction, then loop through every vertex of your mesh and check if your projected line intersects any line formed by two vertices. You would then have more precise values.

EDIT

The second solution requires you to get the vertices of the mesh. Even if your mesh is a circle it is composed of connected points/vertices to draw lines that are connected together to form a circle. You can get these points/vertices via Mesh.vertices. Once you get the array of vertices just take them two by two (v[0] with v[1], then v[1] with v[2]... and so on) to get lines you can interesect with. Once you get a line (let say v[0] and v[1]) you can project a line from your mouse position in the direction you want (left for example). And using the functions given in the Wikipedia page above, you ca get an intersection point (or not). I you get a point (let's call it inter_point), it will mean that this segment of your mesh (v[0] and v[1]) is the left point you're looking for. You can now compute its distance from the mouse position by Vector3.Distance(inter_point, mouse_position). Et voila.

Please not that for a very complex mesh, you may have several segment that would intersect your direction line, just take the further or closer distance according to your needs.

• Can you explain the 2nd option? What will happen if the mesh is an arc? Commented Oct 7, 2019 at 9:39
• I updated my answer. Commented Oct 7, 2019 at 11:01