- Home /
Converting screen coordinates to world coordinates
I have a very basic problem that I cannot find an answer for anywhere. I'm making a snake style game with a few twists for android. The issue is that whenever I touch on screen, my "snake" will only go in two directions it thinks I'm touching way off the screen in the world coordinates.
Here I tried using the only convert method I could find, however this makes it so the the touch is converted to where the camera is which is the center of the screen.
var touch : Touch;
var worldTouchPos : Vector3;
for (var i = 0; i < Input.touchCount; ++i) {
touch = Input.GetTouch(i); ~~~~
worldTouchPos = camera.main.ScreenToWorldPoint(touch.position);
if(touch.phase == TouchPhase.Began)
{
if(direction == 0 || direction == 2)
{
if(transform.position.y < (worldTouchPos.y)) // Go up or down
{
direction = 1;
}
else
{
direction = 3;
}
}
else // go right or left
{
if(transform.position.x > (worldTouchPos.x))
{
direction = 2;
}
else
{
direction = 0;
}
}
}
}//end for
if(direction == 0)//right
{
transform.Translate(Time.deltaTime, 0, 0);
}
else if (direction == 1)//up
{
transform.Translate(0, Time.deltaTime, 0);
}
else if (direction == 2)//left
{
transform.Translate(-(Time.deltaTime), 0, 0);
}
else if (direction == 3)//down
{
transform.Translate(0,-(Time.deltaTime), 0);
}
If I just use touch.position.x and touch.position.y where worldTouchPos is, then thats where it will go two directions and out of the screen.
How can I make it so my touch is exactly where it is in the game world coordinates?
How would adding a z value move the touch point from where my camera is in the center of the screen to exactly where my touch is?
Here is the code above reformatted :
var touch : Touch;
var worldTouchPos : Vector3;
for (var i = 0; i < Input.touchCount; ++i) {
touch = Input.GetTouch(i); ~~~~
worldTouchPos = camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began) {
if (direction == 0 || direction == 2) {
// Go up or down
if (transform.position.y < (worldTouchPos.y)) {
direction = 1;
} else {
direction = 3;
}
} else {
// go right or left
if (transform.position.x > (worldTouchPos.x)) {
direction = 2;
} else {
direction = 0;
}
}
}
}
if (direction == 0) { //right
transform.Translate(Time.deltaTime, 0, 0);
} else if (direction == 1) { //up
transform.Translate(0, Time.deltaTime, 0);
} else if (direction == 2) { //left
transform.Translate(-(Time.deltaTime), 0, 0);
} else if (direction == 3) { //down
transform.Translate(0,-(Time.deltaTime), 0);
}
@tanoshimi had a answer for you 5 hours ago. For a perspective camera, the 'z' parameter of the position passed must be set to the distance in front of the camera. If you pass 0 for 'z', then ScreenToWorldPoint() returns the position of the camera. For example, if your 'action' is occurring at z = 0, and you have your standard 2D setup of the camera at z=-10, then the 'z' parameter would need to be set to 10. If your camera is at an angle with respect to your playing surface, then you need an alternate solution.
I may have confused the issue by posting his code redone above, I only did it because I saw he had posted again below 20 $$anonymous$$s ago, and looking at the original formatting pained me. Sorry... :S
Thanks $$anonymous$$rSoad!!! I am confused still why the value 10 makes sense logically.
Answer by ExtremePowers · Nov 09, 2014 at 08:47 PM
This has similar results to just using touch.position.x and touch.position.y. $$anonymous$$y player goes off the screen as it thinks the touch is off the screen to the top right. It seems like ScreenToWorld is supposed to work but my touches are registered as where the camera is in the center of the screen so my player will circle the center of the screen no matter where I touch.
Your answer
Follow this Question
Related Questions
How to properly implement snake style controls for android? 0 Answers
I Need Help With Mobile Touch Movement. 0 Answers
How long should it take me to make my game 2 Answers
touch position to world position 2D 2 Answers
Best way to do touch in with Unity2D? 0 Answers