1.新建两个layer,一个为Ground,一个为MapSign
2.新建一个camera,命名为MiniMapCamera,将Culling Mask设置为Ground和MapSign,将Projection设置为Orthographic,并让它俯视地面
3.新建一个Render Texture,并将它赋值给MiniMapCamera中的Target Texture
4.新建一个RawImage,将Render Texture赋值给Texture
5.新建一个Image,将一张圆形遮罩图赋值给它,为Image添加Mask组件,并让RawImage成为它的子物体
如无意外,你将看到类似这样的画面:
6.最后,为MiniMapCamera添加一个脚本,负责跟随角色并且不让边界以外的东西显示出来即可!另外,如果显示的地图标记是箭头,只需将箭头成为角色的子物体,就可以无需计算箭头的转向。
using UnityEngine;
using System.Collections;
public class MiniMapCameraFollow : MonoBehaviour {
private Transform playerTra;
private float xMin;
private float xMax;
private float zMin;
private float zMax;
// Use this for initialization
void Start ()
{
//自动计算边界值
GameObject player = GameObject.FindGameObjectWithTag("Player");
GameObject ground = GameObject.FindGameObjectWithTag("Ground");
playerTra = GameObject.FindGameObjectWithTag("Player").transform;
Vector3 playerSize = player.GetComponent<MeshFilter>().mesh.bounds.size;
Vector3 groundSize = ground.GetComponent<MeshFilter>().mesh.bounds.size;
Vector3 playerScale = player.transform.localScale;
Vector3 groundScale = ground.transform.localScale;
float cameraSize = GameObject.FindGameObjectWithTag("MiniMapCamera").GetComponent<Camera>().orthographicSize;
float x = groundSize.x * groundScale.x / 2 - playerSize.x * playerScale.x / 2 - cameraSize / 2;
float z = groundSize.z * groundScale.z / 2 - playerSize.z * playerScale.z / 2 - cameraSize / 2;
xMin = -x + ground.transform.localPosition.x;
xMax = x + ground.transform.localPosition.x;
zMin = -z + ground.transform.localPosition.z;
zMax = z + ground.transform.localPosition.z;
}
// Update is called once per frame
void Update ()
{
float x = Mathf.Clamp(playerTra.localPosition.x,xMin,xMax);
float z = Mathf.Clamp(playerTra.localPosition.z,zMin,zMax);
transform.position = new Vector3(x,5,z);
}
}