本文主要是介绍[Unity学习] 背景视差因子计算,远近背景移动距离差,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public Camera cam;public Transform trans;Vector2 startPosiotion;float startZ;//此物距离trans的z距离private float zDistance => transform.position.z - trans.transform.position.z;void Start(){startPosiotion = transform.position;startZ = transform.position.z;}// Update is called once per framevoid Update(){// 相机移动距离减初始距离,获取相机偏移距离Vector2 camMove = (Vector2)cam.transform.position - startPosiotion;//获取人物前还是后,相机前用近裁剪面,后用远裁剪面float clippingPlane = cam.transform.position.z + (zDistance > 0 ? cam.farClipPlane : cam.nearClipPlane);//计算视差因子float parallaxFactor = Mathf.Abs(zDistance) / clippingPlane;//获取背景需要偏移的x,y位置Vector2 backgroundPos = startPosiotion + camMove * parallaxFactor;//因为是2d,所以z不变Vector3 newPosition = new Vector3(backgroundPos.x, backgroundPos.y, startZ);transform.position = newPosition;}
转载至 链接
这篇关于[Unity学习] 背景视差因子计算,远近背景移动距离差的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!