使用 ScreenToWorldPoint 转换鼠标(触摸)位置不起作用
Converting mouse (touch) position with ScreenToWorldPoint doesn't work
我尽可能地用谷歌搜索,但没有解决方案。
目的:将触摸坐标从像素(屏幕左下角x0)更改为3D环境中的游戏坐标,虽然我不需要Z位置,因为相机没有移动,我只需要跟踪 X 位置,在当前状态下,我的点击(触摸)始终处于正 X。
代码:
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
touchPos.z = 3;
if (touch.phase == TouchPhase.Stationary)
{
if (touch.position.x < 0)
turnSpeed = 8f;
else if (touch.position.x > 0)
turnSpeed = -8f;
}
if (touch.phase == TouchPhase.Ended)
{
turnSpeed = 0f;
}
}
}
相机位置:x:0.027 y:3.847 z:-2.238。我试过设置 touchPos.z = 从 -3 到 10.,但没有成功。
谢谢 ,解决方法如下:
将If (touch.position.x < 0)
改为if (touchPos.x < 0);
在调用 ScreenToWorldPoint 之前或调用时设置 Z 值。我去了:
Vector3 touchPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0.3f));
我尽可能地用谷歌搜索,但没有解决方案。
目的:将触摸坐标从像素(屏幕左下角x0)更改为3D环境中的游戏坐标,虽然我不需要Z位置,因为相机没有移动,我只需要跟踪 X 位置,在当前状态下,我的点击(触摸)始终处于正 X。
代码:
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
touchPos.z = 3;
if (touch.phase == TouchPhase.Stationary)
{
if (touch.position.x < 0)
turnSpeed = 8f;
else if (touch.position.x > 0)
turnSpeed = -8f;
}
if (touch.phase == TouchPhase.Ended)
{
turnSpeed = 0f;
}
}
}
相机位置:x:0.027 y:3.847 z:-2.238。我试过设置 touchPos.z = 从 -3 到 10.,但没有成功。
谢谢
将
If (touch.position.x < 0)
改为if (touchPos.x < 0);
在调用 ScreenToWorldPoint 之前或调用时设置 Z 值。我去了:
Vector3 touchPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0.3f));