我有一个不断向前移动的游戏对象,我使用下面的脚本在 X 轴上用鼠标控制它,并且相机正在跟随这个对象。
我遇到的问题是,当让相机在 X 轴上跟随它时,对象的运动变得很奇怪(因为我认为 WorldToScreenPoint 位置发生了变化),但是在冻结相机的 X 位置时它工作得很好。
我的问题是如何让相机在 X 轴上跟随这个物体而不影响运动?
if ((Input.GetMouseButtonDown(0) || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z));
}
if (((Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0)) || ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)) && !(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)))
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z);
Vector3 curPosition = new Vector3((Camera.main.ScreenToWorldPoint(curScreenPoint).x + offset.x), 0, 0);
float newX = Mathf.Clamp(curPosition.x, -2, 2);
transform.position = Vector3.Lerp(transform.position, new Vector3(newX, transform.position.y, transform.position.z), 25 * Time.deltaTime);
}