Hello everyone,
I’m currently trying to make my game a bit more intuitive and therefore wanted to add the ability to use it via touch input. I have a very basic camera orbit script, with which you can turn the camera around a specific object. Now that I had mouse controls going, I wanted to add the touch component and went for the pinch-zoom first: (Note: All controls are in LateUpdate() )
if (Input.touchCount == 2)
{
touchCount.text = "Touches: 2 - zoom";
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
desiredDistance += deltaMagnitudeDiff * 0.01f * Mathf.Abs (desiredDistance);
All in all, this is pretty basic and made with the help of the docu from Unity, and it works really well. (Note that touchCount is a text label in my UI just for developing reasons)
Anyway, as I tried to add touch movement, I was getting problems, like really weird problems. First, I tried to adapt my new wisdom of touch input in Unity and tried this:
touchCount.text = "Touches: 1 - position";
Touch myTouch = Input.GetTouch(0);
Vector2 myTouchMove = myTouch.position - myTouch.deltaPosition;
xDeg += myTouchMove.x * xSpeed;
yDeg -= myTouchMove.y * xSpeed;
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
rotation = Quaternion.Lerp(currentRotation, desiredRotation, 0.02f * zoomDampening);
transform.rotation = rotation;
But touch wasn’t working until I deactivated the zooming part in my script.
It should make the exact same thing as with the zooming part, but it doesn’t. I can see, thanks to the touchCount output, that the touch isn’t really recognized as it is with the zooming part. Instead of updating every frame, it only switches from 0 touches to 1 touch when I release my finger from the touchscreen, and then it makes a huge jump. At first I thought this jump could be a matter of adjusting parameters for the lenght of my direction vector, but it wasn’t since the movement of my finger does not have any influence on the direction and the lenght of the jump. In fact, a single tap lets the camera jump around in different directions, and as far as it wants.
I also tried another suggested method with TouchPhases: (firstpoint and lastpoint are declared outside the function)
if(Input.GetTouch(0).phase == TouchPhase.Began){
firstpoint = Input.GetTouch(0).position;
}
if(Input.GetTouch(0).phase == TouchPhase.Moved){
secondpoint = Input.GetTouch(0).position;
xDeg += firstpoint.x - secondpoint.x;
yDeg -= firstpoint.y - secondpoint.y;
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
desiredRotation = Quaternion.Euler (yDeg, xDeg, 0);
currentRotation = transform.rotation;
rotation = Quaternion.Lerp (currentRotation, desiredRotation, 0.02f * zoomDampening);
transform.rotation = rotation;
But it wouldn’t work either.
I googled for any problems with touch and Windows and found many topics, but most of them said that multitouch wouldn’t work with Unity (3.x and 4.x mainly), but in Unity 5 it should do. Well, I obviously don’t have a problem with multitouch, more with single touch!
Does anyone know, why Unity detects two touch positions without a problem, but one touch position (or deltaPosition) causes so many problems?
I am aware of the fact that it also can be a rookie-mistake by myself, so please let me know! ![]()
EDIT: Totally forgot: Unity Version 5.1.2f1 Personal, Windows 8.1 with touchscreen display (Acer, as far as I know and if this makes any difference)
Greetings!