Saturday, May 4, 2024
HomeGame Developmentunity - Calculate Mesh top, bottom, left and right position relative to...

unity – 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 enter image description here

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments