- Home /
How to clamp the positions of icons to the edge of a masked circular minimap?
I am trying to make a minimap in my game and i have successfully made one using the below code. But I want to mask it in a circle . I am facing the issue when it comes to clamping the position of the icons on the edge of the Circular minimap. I have tried everything and couldn't get it done. I would appreciate the code itself.(20 and 21 are the offsets of my minimap raw image from its anchor position).
void DrawMapIcons(){
foreach (MapOject mo in mapObjects) {
Vector2 objPos = new Vector2 (mo.owner.transform.position.x, mo.owner.transform.position.y);
Vector2 carPos = new Vector2 (PlayerPos.position.x, PlayerPos.position.y);
if (Vector2.Distance (objPos, carPos) > 100) {
mo.icon.enabled = false;
} else if (mo.owner.activeSelf) {
mo.icon.enabled = true;
}
Vector3 screenpos = minimapCam.WorldToViewportPoint (mo.owner.transform.position);
// Debug.LogError (mo.owner.name + screenpos); mo.icon.transform.SetParent (this.transform);
RectTransform rt = this.GetComponent<RectTransform> ();
Vector3[] corners = new Vector3[4];
rt.GetLocalCorners(corners);
for (int i = 0; i < corners.Length; i++) {
Debug.Log (corners [i]);
}
// Debug.Log (rt.rect.width);
screenpos.x = Mathf.Clamp(screenpos.x * rt.rect.width + 21, corners[0].x + 20, corners[2].x + 20);
screenpos.y = Mathf.Clamp(screenpos.y * rt.rect.height + 21, corners[0].y + 20, corners[1].y + 20);
screenpos.z = 0;
mo.icon.transform.position = screenpos;
}
}
Comment