繁体   English   中英

Unity2D相机根据玩家与物体的距离缩小,当它进一步远离时放大

[英]Unity2D camera zoom out by the distance of player to an object, Zoom in when it gets further away

我正在制作一个侧卷轴2D平台游戏,我有一个时刻,在背景中有一个焦点,我想向玩家展示一个“aw”时刻,我想缩小相机到显示那一刻然后再放大。 我猜你可以说像Death's Gambit或空心骑士一样,当他们想要展示的物体对于相机的大小来说太大了。

试图从某个区域的玩家获得变换并计算到我希望相机大小达到最大值的点的距离,然后计算出一个百分比来加上它乘以大小差异但它会打破查看端口使其显示或缩小到允许的最大统一。

预期的结果是使相机缩小到距离世界某个点的距离,通过该点它停止缩小,向后移动应该使用初始OrtographicSize缩放回播放器。

感谢您的帮助!

你可以做:

public Transform Target1;
public Transform Target2;

public float sensetivity = 1f;
public float maxOrthographicSize = 10f;

Camera thisCam;
float minOrthographicSize = 5f;

private void Start() {
    thisCam = GetComponent<Camera>();
    minOrthographicSize = thisCam.orthographicSize;
}
void Update()
{
    thisCam.orthographicSize = minOrthographicSize + Mathf.Clamp(Vector2.Distance(Target1.position, Target2.position) * sensetivity, 0f, maxOrthographicSize);
}

在此解决方案中, minOrthographicSize只有在Target1Target2处于相同位置时才会发生,以便更改您可以执行的操作:

public Transform Target1;
public Transform Target2;

public float minOrhtographicDistance = 5f;
public float sensetivity = 1f;
public float maxOrthographicSize = 10f;

Camera thisCam;
float min = 5f;

private void Start() {
    thisCam = GetComponent<Camera>();
    min = thisCam.orthographicSize;
}
void Update()
{
    thisCam.orthographicSize = min + Mathf.Clamp((Vector2.Distance(Target1.position, Target2.position) - minOrhtographicDistance) * sensetivity, 0f, maxOrthographicSize);
}

在此解决方案中,正交尺寸将为minOrthographicSize用于minOrhtographicDistance距离,然后将根据目标之间的距离而改变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM