计算出Unity中场景的长于宽,计算出人物在场景中的X与Y轴,然后计算X与Y的位置在场景大地图上的比例,按照比例给小地图赋值即可。
public class UIMap : MonoBehaviour
{
//左下角的点
[SerializeField]
Transform Map_one;
//右上角的点
[SerializeField]
Transform Map_Two;
//获取人物
[SerializeField]
Transform player;
//获取地图上面的红点
[SerializeField]
RectTransform mao_Point;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
SmallMap();
}
void SmallMap()
{
//获取大地图的长和宽
float mapLength = Mathf.Abs(Map_one.position.x - Map_Two.position.x);
//宽
float mapwidth = Mathf.Abs(Map_one.position.z - Map_Two.position.z);
//获取小地图的长和宽
float smallMapLength = GetComponent<RectTransform>().rect.height;
//长
float smallMapwidth = GetComponent<RectTransform>().rect.width;
//获取玩家在大地图中的坐标
float PlayX = player.position.x;
float PlayY = player.position.z;
//获取玩家位置在大地图上的比例
float scaleX = PlayX / mapLength;
float scaleY = PlayY / mapwidth;
//计算地图上的红点在小地图的比例
float PointX = scaleX * smallMapwidth;
float PointY = scaleY * smallMapLength;
//让地图上的红点实时跟随人物的变化而变化
mao_Point.localPosition = new Vector2(PointX, PointY);
}