Gabe
Gabe

Reputation: 644

Why isn't my touch screen input working on Android?

I wanted to make a little game with touch inputs and this is the code I made for it: (This is inside the Update method)

//Check if Input has registered more than zero touches
            if(Input.touchCount > 0){
                //Store the first touch detected.
                Touch myTouch = Input.touches[0];
                //Check if the phase of that touch equals Began
                if (myTouch.phase == TouchPhase.Began)
                {
                    //If so, set touchOrigin to the position of that touch
                    touchOrigin = myTouch.position;

                    if(touchOrigin.x < -2){
                        horizontalInput = -1;
                    } else if(touchOrigin.x > 2){
                        horizontalInput = 1;
                    }
                    if(touchOrigin.x > -2 && touchOrigin.x < 2 && touchOrigin.y < 0){
                        verticalInput = 1;
                    }
                } else if(myTouch.phase == TouchPhase.Ended){
                    horizontalInput = 0;
                    verticalInput = 0;
                }

            }

This is my scene: https://i.sstatic.net/PGbd3.jpg When I tried it on my android phone it only moved to the right and I don't know why. (My camera's position is: x: -6,6; y:-2,6) And I'm using an orthographic camera

Upvotes: 0

Views: 295

Answers (1)

Venkat at Axiom Studios
Venkat at Axiom Studios

Reputation: 2516

This is becase you're using Touch.position, which returns values based on SCREEN space, rather than WORLD space.

What you need to do is convert the point from Screen to World space before applying your conditions. You can do that by using Camera.ScreenToWorldPoint.

Upvotes: 1

Related Questions