设置相机跟随游戏目标:
1.保持相机位置始终跟随一个游戏目标
在Camera下创建Script:
public Transform targetTransform;//获得要跟随的目标位置
public Vector3 offset; //获得Camera与Target的位置偏移
Start(){ offset=transform.position-targetTransform.position; }
Update(){ tranform.position=targetTransform.position+offset; }
2.保持相机在两个目标位置中间
public Transform player1;
public Transform player2;
private Vector3 offer;
private Camera camera;
void Start () {
offer = transform.position - (player1.position+player2.position)/2;
camera = this.GetComponent<Camera>();
}
void Update () {
if (player1 == null || player2 == null) return;
transform.position = (player1.position + player2.position) / 2 + offer;
float distance = Vector3.Distance(player1.position, player2.position);
float size = distance * 0.58f; //镜头距离比例
camera.orthographicSize = size;
}
3.Camera跟随,但有范围
private GameObject gameObject=GameObject.FindGameObjectWithTag("__");
private Transform transform=gameObject.transform;
Vector3 targetPosition = transform.position;
float y = targetPos.y - _f;
if (y > _f) { y = _f; }
if (y < _f) { y = _f;}
this.transform.position = new Vector3(targetPos.x+_f,y,_f);