c# - 将 WorldToScreenPoint 与移动相机一起使用时出现奇怪的移动
问题描述
我有一个不断向前移动的游戏对象,我使用下面的脚本在 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);
}
解决方案
推荐阅读
- java - 监视数据库更改的最佳方法
- reactjs - React-Mobx 2020. 注入钩子和 useObserver
- docker - 有没有一种简单的方法来测试是否需要“docker build”?
- c++ - 如何使用 boost 获取当前文件路径?
- docker - Docker unable to login to Nexus docker registry (hosted)
- javascript - 如何在不使用 Refs 的情况下从父级调用子级的方法?
- c# - Kustomer API,如何在对话中添加附件
- python - 为什么 numpy.array() 使用比 python 列表更多的 RAM?
- windows - 如何使用 QT 和 CMake 在调试模式下启用控制台窗口并在发布模式下禁用?
- python - 当多个属性是AWS lambda python环境中的字符串变量时,如何插入dynamodb表?