- Home /
unify touch position with player position
so i'm trying to move my snake into the directions depending on my touch position but the console gives large number for the touch position and the player cannot move correctly. i want to find a way to convert the pixel in the screen to units for the player to move in the desired direction. \
public void DirectSnake()
{
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Ended)
{
if (touch.position.y > HeadBone.position.y && Dir != Direction.Up && Dir != Direction.Down)
{
HeadBone.eulerAngles = new Vector3(0, 0, 90);
Dir = Direction.Up;
Move();
Debug.Log ("touch position: " + touch.position);
//Debug.Log ("snake position" + HeadBone.position);
return;
}
if (touch.position.y < HeadBone.position.y && Dir != Direction.Up && Dir != Direction.Down)
{
HeadBone.eulerAngles = new Vector3(0, 0, -90);
Dir = Direction.Down;
Move();
Debug.Log ("touch position: " + touch.position);
//Debug.Log ("snake position" + HeadBone.position);
return;
}
if (touch.position.x < HeadBone.position.x && Dir != Direction.Left && Dir != Direction.Right)
{
HeadBone.eulerAngles = new Vector3(0, 0, 180);
Dir = Direction.Left;
Move();
Debug.Log ("touch position: " + touch.position);
//Debug.Log ("snake position" + HeadBone.position);
return;
}
if (touch.position.x > HeadBone.position.x && Dir != Direction.Left && Dir != Direction.Right)
{
HeadBone.eulerAngles = new Vector3(0, 0, 0);
Dir = Direction.Right;
Move();
Debug.Log ("touch position: " + touch.position);
//Debug.Log ("snake position" + HeadBone.position);
return;
}
}
}
}
Comment
Best Answer
Answer by SunnyChow · Apr 13, 2018 at 02:10 AM
all the mouse/touch position is screen position, you need to convert it to world position. Create a plane with collider, and use Camera.ScreenPointToRay() to get the world position on that plane