如何通过扩大视野,以保持鉴于两个对象在所有的时间? (或Z&安培; Y轴) [英] How to keep 2 objects in view at all time by scaling the field of view? (or z&y axis)
本文介绍了如何通过扩大视野,以保持鉴于两个对象在所有的时间? (或Z&安培; Y轴)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了2名球员有点街机射击游戏,而且需要有重点2玩家在屏幕上,我得到了相机在X轴的球员中间移动,但我认为它会很酷当2玩家获得更紧密的摄像头也接近
I'm making a little arcade shooter for 2 players, and need to have the screen focused on 2 players, I got the camera moving in the center of the players in the X axis, but it I think it would be cool when the 2 players get closer together the camera also get closer.
这是透视POV:
This is the perspective pov:
推荐答案
移动相机是不是改变视野更好。计算相机间的距离公式是
Moving the camera is better than changing the fov. The formula for calculating the camera distance is
cameraDistance = (distanceBetweenPlayers / 2 / aspectRatio) / Tan(fieldOfView / 2);
请注意球员出现在一些这样小幅度可以添加视口的边缘。这里是我的脚本再次:
Note the players appear on the very edge of the viewport thus some small margin could be added. Here is my script again:
public Transform player1;
public Transform player2;
private const float DISTANCE_MARGIN = 1.0f;
private Vector3 middlePoint;
private float distanceFromMiddlePoint;
private float distanceBetweenPlayers;
private float cameraDistance;
private float aspectRatio;
private float fov;
private float tanFov;
void Start() {
aspectRatio = Screen.width / Screen.height;
tanFov = Mathf.Tan(Mathf.Deg2Rad * Camera.main.fieldOfView / 2.0f);
}
void Update () {
// Position the camera in the center.
Vector3 newCameraPos = Camera.main.transform.position;
newCameraPos.x = middlePoint.x;
Camera.main.transform.position = newCameraPos;
// Find the middle point between players.
Vector3 vectorBetweenPlayers = player2.position - player1.position;
middlePoint = player1.position + 0.5f * vectorBetweenPlayers;
// Calculate the new distance.
distanceBetweenPlayers = vectorBetweenPlayers.magnitude;
cameraDistance = (distanceBetweenPlayers / 2.0f / aspectRatio) / tanFov;
// Set camera to new position.
Vector3 dir = (Camera.main.transform.position - middlePoint).normalized;
Camera.main.transform.position = middlePoint + dir * (cameraDistance + DISTANCE_MARGIN);
}
这篇关于如何通过扩大视野,以保持鉴于两个对象在所有的时间? (或Z&安培; Y轴)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文