My UI Image(Viewport) wont stay inside parent(MapImage);
So im been trying to make a rts minimap for my rts game, and i got most of the code done but im kind of stuck on how to limit the Viewport inside the map, it even moves out of the canvas. im kind of new to unity so hope you can help me out
Corner1 and Corner2 is gameobject inside the terrain, where Corner 1 is at bottom left of the terrain and corner2 is top right.
This is my code
public RectTransform ViewPort;
public Transform Corner1, Corner2;
public GameObject BlipPrefab;
public static Map Current;ss
public Vector2 mapborder;
private Vector2 terrainSize;
public RectTransform ParentRT;
private RectTransform mapRect;
public Map()
{
Current = this;
}
// Use this for initialization
void Start()
{
terrainSize = new Vector2(
Corner2.position.x - Corner1.position.x,
Corner2.position.z - Corner1.position.z);
mapRect = GetComponent<RectTransform>();
ViewPort.SetParent(mapRect.transform);
ViewPort.localPosition = new Vector3(0, 0, 0);
}
public Vector2 WorldPositionToMap(Vector3 point)
{
var pos = point - Corner1.position;
var mapPos = new Vector2(
point.x / terrainSize.x * mapRect.rect.width,
point.z / terrainSize.y * mapRect.rect.height);
return mapPos;
}
// Update is called once per frame
void Update()
{
ViewPort.position = WorldPositionToMap(ViewportLocation.transform.position);
}
Comment
Your answer
Follow this Question
Related Questions
I can see 3D text thru objects 0 Answers
Maximum number of hexagons in one map? 1 Answer
Get Long Lat From MapNav javascript 0 Answers
Randomly generate blocks on a flat map 0 Answers
MapNav - Geolocation Toolkit Gives Wrong Long and Lat 0 Answers