I am curious how to make a pinch functionality in my game. I assume i have to compare two positions of each finger to see if it is meant to be a pinch in or out. How would i do this?
What can i all do with Input.GetTouch(0).deltaPosition? (0 and 1 respectively for each finger)
Would GetTouch(0).position work how i want (current position as a vector2), but delta gives how much it moved last update call (as a vector2)?
How would i get the previous position of each touch to compare if each finger moved towards or away form each other?
The code i have so far is this:
using UnityEngine;
using System.Collections;
public class CameraZoomPinch : MonoBehaviour
{
public float speed = 0.1F;
public Camera selectedCamera;
private Vector2 touch0pos = new Vector2 (0,0);
private Vector2 touch1pos = new Vector2 (0,0);
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.touchCount == 2)
{
touch0pos = Input.GetTouch(0).position;
touch1pos = Input.GetTouch(1).position;
}
if (Input.touchCount == 2 Input.GetTouch(0).phase == TouchPhase.Moved Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition0 = Input.GetTouch(0).deltaPosition;
Vector2 touchDeltaPosition1 = Input.GetTouch(1).deltaPosition;
//if ((touchDeltaPosition0.magnitude.CompareTo touchDeltaPosition1.magnitude ) < 0.0F)
//{
//selectedCamera.fieldOfView = selectedCamera.fieldOfView = selectedCamera.fieldOfView + 1;
//}
if ((Vector2.Distance(touch0pos,touch1pos)-(Vector2.Distance(touchDeltaPosition0,touchDeltaPosition1)) < 0.0F))
{
//selectedCamera.transform.Translate(0, 0, (touchDeltaPosition0.magnitude - touchDeltaPosition1.magnitude) * speed);
selectedCamera.fieldOfView = selectedCamera.fieldOfView + 1;
}
}
}
}
Right now my code just makes my camera fov increase only positively. Are the delta magnitude values always positive? How is direction represented?
I chose an inspector helped variable for my camera as i believe that is faster than selecting the object each time iteratively.
I also want to figure out rotation of two fingers to rotate. Would i have to track each finger, and figure out the angle they move each second (delta angle with some trig function) and then apply that to my camera’s local euler angle?
How would i do these tasks? Can somebody provide code examples. I learn best form looking at code, but i cannot find examples of delta and making pinches, even though it seems to be a common question. I was also disappointed the unity documentation wasn’t as thorough as I’d like it to be on using delta other than just deltaTime and with examples.
I have tried searching these forums, but no luck on finding HOW to do it in c# (if i saw javascript i could see the idea how to do it) but nobody has gotten code to be given yet people keep asking…