Drawing a box with mouse dragged on screen

When a user clicks mouse0 I set a

Vector2 orgBoxPos = Input.mousePosition;

While he is holding down mouse0, I constantly update

Vector2 endBoxPos = Input.mousePosition;

I then try this to draw this box on the screen (ie: imagine an RTS style selection box):

GUI.DrawTexture(new Rect(orgBoxPos.x, Screen.height - orgBoxPos.y, endBoxPos.x - orgBoxPos.x, Screen.height - endBoxPos.y - orgBoxPos.y), selectTexture);

All of the Rect coords are correct except the last one (Remember unity GUI y coords are inverted, for some funky reason).

For some reason I can’t seem to figure out how to correctly calculate the end height of my rectangle.

Any help would be greatly appreciated!

Not sure if alexkaskasoli’s answer works or not, but I think they outer if should be the Input.GetKey() which is called while the key is down. So my solution is.

/// <summary>
/// Handles the case where the user draws a rectangle to select some units.
/// </summary>
void Update ()
    // Called while the user is holding the mouse down.
        // Called on the first update where the user has pressed the mouse button.
        if (Input.GetKeyDown(KeyCode.Mouse0))
            _box_start_pos = Input.mousePosition;
        else  // Else we must be in "drag" mode.
            _box_end_pos = Input.mousePosition;    
        // Handle the case where the player had been drawing a box but has now released.
        if(_box_end_pos != Vector2.zero && _box_start_pos != Vector2.zero)

        // Reset box positions.
        _box_end_pos = _box_start_pos = Vector2.zero;

/// <summary>
/// Draws the selection rectangle if the user is holding the mouse down.
/// </summary>
void OnGUI()
    // If we are in the middle of a selection draw the texture.
    if(_box_start_pos != Vector2.zero && _box_end_pos != Vector2.zero)
        // Create a rectangle object out of the start and end position while transforming it
        // to the screen's cordinates.
        var rect = new Rect(_box_start_pos.x, Screen.height - _box_start_pos.y,
                            _box_end_pos.x - _box_start_pos.x,
                            -1 * (_box_end_pos.y - _box_start_pos.y));
        // Draw the texture.
        GUI.DrawTexture(rect, SelectionTexture);

Found it in case anyone needs it:

How to draw a RTS rectangle using Mouse Coordinates

private Vector2 orgBoxPos = Vector2.zero;
private Vector2 endBoxPos = Vector2.zero;

if (Input.GetKeyDown(KeyCode.Mouse0)) {
		if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {

			orgBoxPos = Input.mousePosition;
		else if (Input.GetKey(KeyCode.Mouse0)) {
			endBoxPos = Input.mousePosition;
else {
   orgBoxPos = Vector2.zero;
   endBoxPos = Vector2.zero;

void OnGUI() {
 if (orgBoxPos != Vector2.zero && endBoxPos != Vector2.zero) {
	GUI.DrawTexture(new Rect(orgBoxPos.x, Screen.height - orgBoxPos.y, endBoxPos.x - orgBoxPos.x, -1 * ((Screen.height - orgBoxPos.y) - (Screen.height - endBoxPos.y))), selectTexture); // -