Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Bug in RectInt.ClampToBounds?

Discussion in 'Editor & General Support' started by Lorin_Atzberger, Dec 31, 2022.

  1. Lorin_Atzberger

    Lorin_Atzberger

    Joined:
    Dec 11, 2013
    Posts:
    119
    So I've been using this one for a while now and I've hit a weird behavior.

    The documentation says:
    "Clamps the position and size of the RectInt to the given bounds."

    From this I understand a sort of intersection between two rectangles where the area of the first is clamped to fit in the area of the second.

    The code for it seems to be shown like this in Rider
    Code (CSharp):
    1.  
    2. public void ClampToBounds(RectInt bounds)
    3. {
    4.   this.position = new Vector2Int(Math.Max(Math.Min(bounds.xMax, this.position.x), bounds.xMin), Math.Max(Math.Min(bounds.yMax, this.position.y), bounds.yMin));
    5.   this.size = new Vector2Int(Math.Min(bounds.xMax - this.position.x, this.size.x), Math.Min(bounds.yMax - this.position.y, this.size.y));
    6. }
    7.  
    This seems to behave like an intersection if the position/min of the first rect is contained in the second rect, but if the position/min of the first rectangle is smaller than the position/min of the second rectangle then the position gets clamped within the area, but the size seems to mostly remain the same.

    Do I have a fundamental misunderstanding of how the function is supposed to work or is this a legitimate bug?
     
  2. Lorin_Atzberger

    Lorin_Atzberger

    Joined:
    Dec 11, 2013
    Posts:
    119