- Home /
About touch input in different screen sizes
Hello everyone, I am currently working in a mobile game. Everything was coming along nicely, but when I hit the testing phase I discovered a problem.
Ok, here is how it goes, if you touch the left side of the screen you go left, if you touch the right side of the screen you go right, pretty simple stuff. I tested this on tablets and it works perfectly, however when I deployed it on cell phones or devices with small screens the movement seems to be buggy, what I mean here is that if I use touchphase stationary on tablets it will work fine, but when I use the same on phones then it appears that it is detecting multitapping rather than a single stationary tap. Is anyone else having a similar issue? if so, how did you get it to work properly.
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Stationary && touch.position.x > Screen.width/2 && Input.touchCount < 2)
{
move = 1;
myTransform.Translate(Vector2.right * speed * Time.deltaTime);
anim.SetFloat("Speed", Mathf.Abs(move));
//facingRight = true;
}
else if (touch.phase == TouchPhase.Stationary && touch.position.x < Screen.width/2 && Input.touchCount < 2)
{
move = -1;
myTransform.Translate(Vector2.right * speed * Time.deltaTime);
anim.SetFloat("Speed", Mathf.Abs(move));
//facingRight = false;
}
else
{
move = 0;
}
}
Again, this works perfect on tablets, but does not work as expected on phones.
Answer by benni05 · Apr 06, 2014 at 06:54 PM
You assume that your script ends up in the else section on a phone, right? There could be two reasons for this: either touchCount is 2 or greater OR TouchPhase is not Stationary. Since the display of a phone is significantly smaller it might be that you are not able to keep your finger motionless enough to trigger the Stationary event. Actually there is a third possibility in that you do not test for the case
touch.position.x == Screen.width/2
but it is not very likely that this is your issue ;-) Anyway, for the sake of correctness, change the
touch.position.x < Screen.width/2
to
touch.position.x <= Screen.width/2
I was just posting the snippet for the movement functionality. Touch down can't be two because when it detects that it is 2 the character will stop moving, or at least that is what happens in the tablet when I put two fingers at the same time. I think it would be the second possibility, not being able to keep the finger motionless enough to trigger the stationary event, because as I said it behaves as if I was infinitely tapping the phone.
I also made the changes you suggested, did not fix my problem, but the code looks a hell of a lot better ;)
Ins$$anonymous$$d of testing for Stationary just do not allow to move the finger more than xyz. You can achieve this either by saving the initial touch position on TouchDown in a variable and compare the current value with this (allow for example a deviation of 5 pixels or even better a deviation of i.e. 1% of screen width so make it the same on all devices regardless of display size and resolution). Or omit the Stationary condition and work with Touch.deltaPosition:
http://docs.unity3d.com/Documentation/ScriptReference/Touch-deltaPosition.html
I guess this is not as precise because deltaPosition will return only full pixels (has moved 1 pixel or 2 etc. per frame). Even a 1 pixel movement per frame is quite fast and this is not what you would like to allow I guess. But the way to test it for you could be:
if (Touch.deltaPosition.magnitude <= 1 ...
Your answer
Follow this Question
Related Questions
IPointerEnterHandler / IPointerExitHandler and touch 1 Answer
How to get an object to follow touch when held. (move on y axis) 2 Answers
Problem with multi-touch from android 1 Answer
How do I detect touch on another object and check by tag/name? 1 Answer
How to launch a Hyperlink on Touching a Game Object ? 0 Answers