2D游戏相机跟随
private float smooth = 3;
float posX = transform.position.x;
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, new Vector3(Mathf.Clamp(posX, 0, 15), Camera.main.transform.position.y, Camera.main.transform.position.z),smooth*Time.deltaTime);
3D两人游戏,相机的跟随
public Transform player1;
public Transform player2;
private Vector3 offset;
private Camera camera;
private float rate=0.58f;//相机大小和两个玩家之间距离的比例,比如size=5,距离=9—>rate=0.58,这样可以让相机随着比例放大缩小
private void Start()
{
offset = transform.position - (player1.position - player2.position) / 2; //相机和两个玩家之间的中点距离
camera = GetComponent<Camera>();
}
private void Update()
{
transform.position = (player1.position - player2.position) / 2 + offset; //随时更新相机位置
float size = (float)(Vector3.Distance(player1.position, player2.position) * rate);
camera.orthographicSize = size;//相机视野的大小变化
}