Help With Indicator Pointing to Where Target is

In unity, this is my code for an indicator that also has clamping (it is meant to show where the enemy ( target ) is:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
public class UIFollowTarget : MonoBehaviour {
    RectTransform myRectTransform;
    Camera mainCamera;
    public Transform currentTarget;
    public bool clampToScreen = true;
    [SerializeField] Vector2 clampBorderSize;
    public Vector3 offset;
    void Start(){
        myRectTransform = GetComponent<RectTransform>();
        mainCamera = Camera.main;
    }
    void Update(){
        Vector3 noClampPosition = mainCamera.WorldToScreenPoint(currentTarget.position + offset);
        Vector3 clampedPosition = new Vector3(Mathf.Clamp(noClampPosition.x,0+clampBorderSize.x,Screen.width-clampBorderSize.x),
        Mathf.Clamp(noClampPosition.y,0+clampBorderSize.y,Screen.height-clampBorderSize.y),
        noClampPosition.z);
 
        myRectTransform.position = clampToScreen ? clampedPosition : noClampPosition;
    }
}

However, when the ui clamps to the bottom right or the bottom left side of the screen, it teleports to the top right or top left, so basically the side opposite to the first side. It then hangs around up there until the target comes within the screen, then it fixes back to normal. This happens randomly but mainly with the bottom. When it happens with the top left or top right corner, again, it goes to the opposite side. Why is this? I’ll be active answering any questions if needed.