0
void OnGUI()
    {
        if (selectionStarted)
        {
            Rect rect = GetScreenRect(mousePosition1, Input.mousePosition);
            DrawScreenRectBorder(rect, 2, Color.cyan);

            rectangleReference = rect;
        }
        else
        {
            DrawScreenRectBorder(rectangleReference, 2, Color.cyan);
            rectangleReference.position = new Vector2(myCube.position.x, myCube.position.y);
        }
    }

rectangleReference is Rect and I make a reference to the drawn rect. myCube is a Transform a reference to a 3d cube.

then I tried to change the rectangle position to the cube position but the drawn rectangle is move to the top left position in the screen.

rectangleReference.position = new Vector2(myCube.position.x, myCube.position.y);

The rectangle(rectangleReference) position is vector2 the 3d cube position vector3

This is the method GetScreenREct

Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
    {
        // Move origin from bottom left to top left
        screenPosition1.y = Screen.height - screenPosition1.y;
        screenPosition2.y = Screen.height - screenPosition2.y;
        // Calculate corners
        var topLeft = Vector3.Min(screenPosition1, screenPosition2);
        var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
        // Create Rect
        return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
    }
ben dvivonim
  • 85
  • 1
  • 9
  • 1
    Well `Vector2` is just an `x` and a `y` value while `Vector3` has an additional `z` field. Both are implicitly convertible into each other ... Your actual issue is: `OnGUI` rects use **screen/pixel space** while your 3D object uses world space unit coordinates – derHugo Jan 31 '22 at 07:11
  • 1
    What does `GetScreenRect` do? – derHugo Jan 31 '22 at 07:12
  • @derHugo I updated my question with the GetScreenRect method. – ben dvivonim Jan 31 '22 at 12:18

0 Answers0