Anybody got the pinch gesture up and running?
I coded up a simple 2 touch routine that checks a vector 2 and whether it’s growing or shrinking. I did not see support for gestures built into Unity. I did the same thing for finger flicks, full taps, or slow drags in Debris.
Do you mine sharing the code for pinch Jeremy? I’ve also been struggling how to do this.
LOL, I forgot how much brute force I used when doing this. You can clean it up but the basic principle is there.
if ( iPhoneInput.touchCount == 2 )
{
var touch1 : iPhoneTouch = iPhoneInput.GetTouch( 0 );
var touch2 : iPhoneTouch = iPhoneInput.GetTouch( 1 );
if ( touch1.position.x < touch2.position.x )
Camera.main.transform.position.z -= ( touch1.positionDelta.x - touch2.positionDelta.x ) / 10;
if ( touch1.position.x > touch2.position.x )
Camera.main.transform.position.z += ( touch1.positionDelta.x - touch2.positionDelta.x ) / 10;
if ( touch1.position.y < touch2.position.y )
Camera.main.transform.position.z -= ( touch1.positionDelta.y - touch2.positionDelta.y ) / 10;
if ( touch1.position.y > touch2.position.y )
Camera.main.transform.position.z += ( touch1.positionDelta.y - touch2.positionDelta.y ) / 10;
if ( Camera.main.transform.position.z > -2 )
Camera.main.transform.position.z = -2;
if ( Camera.main.transform.position.z < -15 )
Camera.main.transform.position.z = -15;
}
Thx Jeremy!
Yea man nice job! I’ve been wondering about this too.
I must fundamentally not grok the meaning of GetTouch and the values it returns.
I thought GetTouch(0) would get you the first finger, and GetTouch(1) would get you the second finger touching the screen. Which would mean that comparing the x and y’s of the two would only tell you how far apart your fingers were, but not which way they were moving. Obviously I’m wrong though since this code obviously works fine in Debris.
The docs don’t seem to help me as what scant description there is of GetTouch, it seems to reinforce my (wrong) assumption. Can someone who understands please explain this?
If you’ll notice there are references to position and to delta. Depending on the fingers’ relative positions the delta will tell you if they are diverging or converging.
Ahh, somehow I totally missed that in your code. It all makes sense now. That’s what I get for not getting any sleep. Thanks!
First off, thanks for getting me started on this, Jeremy!
Okay, I realized that taking action on each axis separately resulted in a situation where if your “pinch” took place along a single axis, the camera would move far less than if your “pinch” occurred diagonally (i.e. taking place along both axes) since your camera got moved once for X and again for Y.
To solve this, I used the following approach:
iPhoneTouch touch = iPhoneInput.GetTouch(0);
iPhoneTouch touch2 = iPhoneInput.GetTouch(1);
// Find out how the touches have moved relative to eachother:
Vector2 curDist = touch.position - touch2.position;
Vector2 prevDist = (touch.position - touch.positionDelta) - (touch2.position - touch2.positionDelta);
float delta = curDist.magnitude - prevDist.magnitude;
Then “delta” is what you use to move your camera (either along Z in the case of a perspective projection, or to change the orthographicSize in the case of an orthographic projection).
I hope someone finds that useful.
That looks more like what I originally thought I wrote LOL. That’s sweet!
What would be the most reliable way to check to see if the player was zooming in or zooming out ?
thanks
Check whether delta in my example above is positive or negative.
thanks Brady - did that earlier but the value changes rapidly (set a bool) from true to false even if you are only zooming out
Digs Up Grave
…was wondering, within this context if anyone attempted to figure out how to do ‘rotations’ with two fingers. Essentially the pinch and turn you can do for example in Google Earth (pinch zooms, turning fingers rotates world).
I know this was asked in other threads (that I can’t seem to find) so I figured being that Pinch was “solved”, perhaps there was some movement on rotates.
Hi, all,
Does anyone have a solution for a swipe-to-move camera script?
Thanks,
Greg
To bring it around full circle, here’s what Brady meant in total (since there were a few things not mentioned on where/how to use delta:
if ( iPhoneInput.touchCount == 2 )
{
iPhoneTouch touch1 = iPhoneInput.GetTouch( 0 );
iPhoneTouch touch2 = iPhoneInput.GetTouch( 1 );
// Find out how the touches have moved relative to eachother:
Vector2 curDist = touch1.position - touch2.position;
Vector2 prevDist = (touch1.position - touch1.positionDelta) - (touch2.position - touch2.positionDelta);
float touchDelta = curDist.magnitude - prevDist.magnitude;
// move camera in world coordinates
//Camera.main.transform.position -= new Vector3(0, 0, touchDelta * .5f);
// translate along local coordinate space
Camera.main.transform.Translate(0,0,touchDelta*.5f);
}
The code above will give you what you’re looking for
You can also do this as:
protected boolean touchEvent(TouchEvent message)
{
switch(message.getEvent())
{
case TouchEvent.GESTURE:
TouchGesture gesture = message.getGesture();
switch(gesture.getEvent())
{
case TouchGesture.PINCH_END:
Dialog.alert("Focal point: " + message.getX(1)
- ", " + message.getY(1)
- "\nFinal zoom value: " + gesture.getPinchMagnitude());
return true;
}
}
return false;
}
Hi guys,
I’m trying to use the code posted as above.
function Update ()
{
if ( Input.touchCount == 2 )
{
var touch1 : Touch = Input.GetTouch( 0 );
var touch2 : Touch = Input.GetTouch( 1 );
Vector2 curDist = touch1.position-touch2.position;
Vector2 prevDist = (touch1.position-touch1.positionDelta)-(touch2.position-touch2.positionDelta);
float touchDelta = curDist.magnitude-prevDist.magnitude;
Camera.main.transform.translate(0,0,touchDelta*.5f);
}
}
However, it returns a few error when I was trying to compile it.
Assets/Standard Assets (Mobile)/Scripts/pinchZoom.js(8,24): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Standard Assets (Mobile)/Scripts/pinchZoom.js(9,24): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Standard Assets (Mobile)/Scripts/pinchZoom.js(11,22): UCE0001: ‘;’ expected. Insert a semicolon at the end.
I notice that the semicolon is already in the script.
can anyone show me how to get it work?
thanks.
Digs up from grave
I believe you are getting that error because you added to a .js and the above was C# This solves KFC problem: