Hello fellow unity developers! My brain has hit a road block. I have a decent single tap + swipe script going, however, at the end of any form of swipe movement, it is registers a single tap. Can you provide a recommendation of a better way to recognize a single tap? Below is the code example.
Touch mytouch = Input.GetTouch(0);
if(mytouch.phase == TouchPhase.Ended && mytouch.tapCount == 1){
Debug.Log(“I Tapped that!!”);
}
Untested, but something like the following should work…
Touch mytouch = Input.GetTouch(0);
if (mytouch.phase == TouchPhase.Moved)
{
wasDragged = true;
}
if(mytouch.phase == TouchPhase.Ended && mytouch.tapCount == 1)
{
if (wasDragged)
{
Debug.Log("Drag ended");
}
else
{
Debug.Log("I Tapped that!!");
}
wasDragged = false;
}
Thank you jgodfrey! This got me going. I had to move the “wasDragged=false” piece into an if statement that detected if the touch was 0. Otherwise the moment I stop dragging my finger it registers false and I sit get a “tap” at the end of the swipe.
public class TouchCamera : MonoBehaviour {
Vector2?[] oldTouchPositions = {
null,
null
};
Vector2 oldTouchVector;
float oldTouchDistance;
Vector2 touch;
Vector3 hitPoint;
Touch mytouch;
RaycastHit hit;
bool swipeDected;
void Update() {
Debug.DrawRay(transform.position, transform.forward);
Debug.DrawRay(transform.position, -transform.forward);
Debug.DrawRay(transform.position, -transform.up);
Debug.DrawRay(transform.position, -transform.right);
Debug.DrawRay(transform.position, transform.right);
if (Input.touchCount == 0) {
oldTouchPositions[0] = null;
oldTouchPositions[1] = null;
swipeDected = false;
}
else if (Input.touchCount == 1) {
if (oldTouchPositions[0] == null || oldTouchPositions[1] != null) {
oldTouchPositions[0] = Input.GetTouch(0).position;
oldTouchPositions[1] = null;
}
else {
Touch mytouch = Input.GetTouch(0);
if (mytouch.phase == TouchPhase.Moved){swipeDected = true;}
if(mytouch.phase == TouchPhase.Ended && mytouch.tapCount == 1 && swipeDected == false){
cameraMovement("singletap");
}else{cameraMovement("swipe");}
}
}
else {
if (oldTouchPositions[1] == null) {
oldTouchPositions[0] = Input.GetTouch(0).position;
oldTouchPositions[1] = Input.GetTouch(1).position;
oldTouchVector = (Vector2)(oldTouchPositions[0] - oldTouchPositions[1]);
oldTouchDistance = oldTouchVector.magnitude;
}
else {
Vector2 screen = new Vector2(camera.pixelWidth, camera.pixelHeight);
Vector2[] newTouchPositions = {
Input.GetTouch(0).position,
Input.GetTouch(1).position
};
Vector2 newTouchVector = newTouchPositions[0] - newTouchPositions[1];
float newTouchDistance = newTouchVector.magnitude;
// -------- Raycast Hit --------
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 300)){}
float distanceToGround = hit.distance;
var pivotPoint = hit.point;
//---------------------------------------------------------
//---------------------------------------------------------
if(distanceToGround < 100){
camera.orthographicSize = distanceToGround + 5;
} else{
camera.orthographicSize = 100;
}
// -------- Pinch Zoom ---------
var pinchDistance = oldTouchDistance - newTouchDistance;
if (pinchDistance > 2 || pinchDistance < -2){
if (distanceToGround < 10){
if (pinchDistance > .1){
transform.Translate(-Vector3.forward * pinchDistance * camera.orthographicSize / screen.y);
}
}
else {
transform.Translate(-Vector3.forward * pinchDistance * camera.orthographicSize / screen.y);
}
}
//---------------------------------------------------------
//---------------------------------------------------------
// -------- Rotation ---------
transform.RotateAround(pivotPoint, Vector3.up, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f);
//---------------------------------------------------------
//---------------------------------------------------------
// -------- 2 Finger Pan Camera ---------
transform.Translate(Vector3.right * ((oldTouchPositions[0].Value[0] + oldTouchPositions[1].Value[0] - screen[0]) * camera.orthographicSize / screen.x));
transform.Translate(-Vector3.right * ((newTouchPositions[0].x + newTouchPositions[1].x - screen[0]) * camera.orthographicSize / screen.x));
//---------------------------------------------------------
//---------------------------------------------------------
oldTouchPositions[0] = newTouchPositions[0];
oldTouchPositions[1] = newTouchPositions[1];
oldTouchVector = newTouchVector;
oldTouchDistance = newTouchDistance;
}
}
}
void cameraMovement(string type){
switch (type) {
case "swipe":
Vector2 newTouchPosition = Input.GetTouch(0).position;
Vector2 screen = new Vector2(camera.pixelWidth, camera.pixelHeight);
// -------- 1 Finger Pan Camera --------
Vector3 forward = camera.transform.forward;
forward.y = 0f;
camera.transform.position += forward * ((oldTouchPositions[0].Value[1] + oldTouchPositions[0].Value[1] - screen[1])) * camera.orthographicSize / screen.y;
camera.transform.position += forward * -((newTouchPosition.y + newTouchPosition.y - screen[1])) * camera.orthographicSize / screen.y;
transform.Translate(Vector3.right * ((oldTouchPositions[0].Value[0] + oldTouchPositions[0].Value[0] - screen[0]) * camera.orthographicSize / screen.x));
transform.Translate(-Vector3.right * ((newTouchPosition.x + newTouchPosition.x - screen[0]) * camera.orthographicSize / screen.x));
//---------------------------------------------------------
//---------------------------------------------------------
oldTouchPositions[0] = newTouchPosition;
Debug.Log(swipeDected);
break;
case "singletap":
touch = Input.GetTouch(0).position;
setHitPoint ();
Debug.Log("I tapped");
break;
case "two_fingers":
break;
}
}
void setHitPoint(){
Ray ray = Camera.main.ScreenPointToRay (touch);
if (Physics.Raycast (ray, out hit)) {
hitPoint = hit.point;
Debug.DrawRay (ray.origin, ray.direction * 500);
//Debug.Log(hitPoint);
}
}
}