- Home /
Problem with Input.touchCount == 1
Howdy Everyone! I have an orthographic camera facing z negative at positive z = 20. I want the player to pan the camera with one finger and zoom the camera when they pinch. So far I have the pinch function working but I can't figure out why my pan function broken. Can anyone tell me what I'm doing wrong?
#pragma strict
import UnityEngine;
import System.Collections;
var speed : int = 4;
var selectedCamera : Camera;
var MINSCALE : float = 2.0F;
var MAXSCALE : float = 5.0F;
var varianceInDistances : float = 5.0F;
private var touchDelta : float = 0.0F;
private var prevDist : Vector2;
private var curDist : Vector2;
private var startAngleBetweenTouches : float = 0.0F;
private var midPoint : Vector2;
function Update ()
{
if (Input.touchCount == 1) //
{
var touchDeltaPosition : Vector2 = Input.GetTouch(0).deltaPosition;
selectedCamera.transform.parent.transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
// droid zoom controls
if (Input.touchCount == 2) // && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
midPoint = Vector2(((Input.GetTouch(0).position.x + Input.GetTouch(1).position.x)/2), ((Input.GetTouch(0).position.y - Input.GetTouch(1).position.y)/2)); //store midpoint from first touches
curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
touchDelta = curDist.magnitude - prevDist.magnitude;
if ((touchDelta < 0)) //
{
selectedCamera.orthographicSize = Mathf.Clamp(selectedCamera.orthographicSize + (1 * speed),2,90);
}
if ((touchDelta > 0))
{
selectedCamera.orthographicSize = Mathf.Clamp(selectedCamera.orthographicSize - (1 * speed),2,90);
}
}
}
What's wrong with it? Does nothing? $$anonymous$$oves too slow/fast? A fun trick is to make a GUIText and print touchCount and deltaPos in it.
You likely copied the code from the reference. Since you are facing negative 'Z' you'll likely have to change the sign of one of the values you are passing to Translate().
In addition touchDeltaPosition is in screen coordinates, so getting an object to match up with your finger movement will be next to impossible, and if you got something close it would only work on a single resolution. That is this will work to move the object in the same general direction as the finger moves, but the movement won't track the finger.
Sorry for the late response. The camera does not move when the user touches with one finger. It should move along the x/y axis and not move the z at all base on how far the user's finger moves (touch delta).
The translate function looks like this Translate(-touchDeltaPosition.x speed, -touchDeltaPosition.y speed, 0);
Since it is a public variable, verify the value of 'speed' in the inspector. If it is set appropriately, put a debug statement between lines 25 and 26:
Debug.Log(touchDeltaPosition);
Answer by DBurge · Jul 22, 2013 at 01:40 PM
I figured it out, the problem was with the translate function
selectedCamera.transform.parent.transform.Translate
should be
selectedCamera.transform.Translate
Thanks for the help!
Your answer
Follow this Question
Related Questions
Scripts don't work when i try app 2 Answers
Jumping with touch input. 0 Answers
Make sphere shoot to Touch.position error 1 Answer
Android build problem! 1 Answer
Unity Touch=2 commends? 1 Answer