Question by
aeffoa · Apr 30 at 07:26 PM ·
camerapositioncoordinates
Converting mouse(touch) position with ScreenToWorldPoint doesn't work.
Hello dear people. Googled as much as i could, no solution. Aim: change touch coordinates from pixels(screen left-bottom corner x0) to game coordinates in 3D environment, although i don't need Z pos because the camera is not moving and i just need to track X pos and with current state my clicks(touches) are always at positive X. Code:
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;
}
}
}
Camera pos: x: 0.027 y: 3.847 z : -2.238 I've tried setting touchPos.z = from -3 to 10., no luck.
Comment
Best Answer
Answer by aeffoa · May 02 at 05:45 PM
The solution was to make two changes:
1) change If (touch.position.x <0) to if (touchPos.x < 0);
2) Set Z value before/when calling ScreenToWorldPoint. I went with Vector3 touchPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 0.3f));
Your answer
![](https://koobas.hobune.stream/wayback/20220613060029im_/https://answers.unity.com/themes/thub/images/avi.jpg)