- Home /
Question by
TheCount · Feb 02, 2014 at 12:57 AM ·
javascriptinputpositiontouch
Get touch position?
All I need is to get the x coordinate of the touch position.
I want to know if the user has tapped on the right half of the screen.
This may not be the only touch on the screen... so here is what I have so far:
#pragma strict
function Update (){
for (var i = 0; i < Input.touchCount; ++i){
if (Input.GetTouch(i).phase == TouchPhase.Began) {
if (Input.GetTouch[i].position > parseFloat(Screen.width/2)){
//do something
}
}
}
}
Can someone help me?
Comment
In order to use it, do you attach it to the camera and does it work for 3 D games
Best Answer
Answer by KellyThomas · Feb 02, 2014 at 01:19 AM
One error in you code is using "`GetTouch[i]"` on line 6, when it should be "`GetTouch(i)`" as demonstrated on the line before it.
Usually if you are repeatedly calling the same getter it is neater and easier to store that value of interest locally.
function Update () {
for (var i = 0; i < Input.touchCount; ++i) {
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began) {
if (touch.position > (Screen.width/2)) {
//do something
}
}
}
}
have a small error in your code.
function Update () {
for (var i = 0; i < Input.touchCount; ++i) {
Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began) {
// Need to put .x
if (touch.position.x > (Screen.width/2)) {
//do something
}
}
}
}
var touch : Touch;
touch = Input.GetTouch(i);
Ins$$anonymous$$d of:
Touch touch = Input.GetTouch(i);
Worked for me. Thanks