##通过两个物体的距离的变化来改变摄像机的尺寸
思路:1、两个物体越远,则摄像机尺寸越大;若两个物体越近,则摄像机尺寸越小(但是要有一个最小尺寸比较好)。2、若是双方可以进行攻击,并且可以摧毁另外一个物体的话,就要设置一个判断,即若有一方消失,则不改变摄像机尺寸。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCamera : MonoBehaviour
{
public Transform Player1;//获取到两个物体之间的位置
public Transform Player2;
private Camera camera;
private Vector3 offset;//坦克中心位置和camera的偏移量
// Start is called before the first frame update
void Start()
{
offset = transform.position - (Player1.position + Player2.position) / 2;//获取偏移量
camera = this.GetComponent<Camera>();
}
// Update is called once per frame
void Update()
{
if (Player1 == null || Player2 == null) return;//若有一方消失,则不执行摄像机的放缩
transform.position = (Player1.position + Player2.position) / 2 + offset;//设置摄像机的位置
float distance = Vector3.Distance(Player1.position, Player2.position);//获取坦克之间的距离
if (distance < 5f) return;//摄像机的最小尺寸
float size = distance * 0.875f;//获取size大小
camera.orthographicSize = size;//把尺寸赋值给摄像机的size
}
}
大家有什么不懂的可以随时评论,我看到就会回答的。希望和大家共同进步!