I’m an artist with limited background in scripting. I’ve been looking online for a js script to enable pinch zoom on ios. It seems simple enough. 1 single script attached to camera to enable this behavior but looking online i can not find good reference or help on how to execute this properly.
I purchased Playmaker in hopes it could be helpful with my limited scripting knowledge however the playmaker dosent seem fitted very well for IOS development.
any help would be greatly appreciated. I’d even pay for the pinch zoom script if need be.
well, I just copied and pasted the part I thought would be relevent. First of all, the coroutine I referenced just moves an ortho camera, but you don’t have a function by that name, right? So nix it or write one with that name. Also, you have a : at the end of the call which is probably where the error comes from. It also looks like you didn’t place it inside a function at all, and that won’t work.
I have that code in a FixedUpdate function so that it is constantly being checked. try that, while replacing the StartCoroutine with something to move the camera away.
Good luck, and use the documentation, you’ll learn so much by troubleshooting why a snippet of code isn’t working in your scenario.
BTW, I didn’t orignally write that code, it was posted here on the forum… I dont want to steal credit that isn’t due
AND one more thing I just realized, I didn’t mention that my code snippet is C#, not js. js doesn’t declare variables that way, and this wont work as js. Let me give it a crack, but I can’t test it.
OK thanks for the rapid response. Still not clear about why this is so difficult to just have a js script apply a pinch zoom to camera. To digest your feedback I need to decode it and trouble shoot.
I’ll buy this script if thats possible?
I looked at your website. let me know if you need any help with art.
it seems someone could make a lot of money selling a bundle of IOS related scripts to enable a designer/ artist basic IOS functionality in a unity app.
Yeah, I’m not an impressive artist. Thanks for the offer, I’ll have to take you up on that sometime.
Certainly no need to pay ME for the script, as I said I didn’t orignally write it.
The pinch zoom isn’t that hard. You check to see if the user has 2 fingers on the screen, if they do, get their positions and get them again as they move (deltaPosition) and check the distance between them… When the distance is small enough(curdist.magnitude - prevdist.magnitude), you can consider it a zoom, so do the zoom specific code.
Here is a script that can zoom a camera by pinching. I just set the FOV (field of view of the camera)
This code does not play well with two finger swipes, as it zooms erratically when swiping, but it works fine for zooming. Not sure to get it to not zoom when swiping.
using UnityEngine;
using System.Collections;
public class CameraZoomPinch : MonoBehaviour
{
public int speed = 4;
public Camera selectedCamera;
public float MINSCALE = 2.0F;
public float MAXSCALE = 5.0F;
public float varianceInDistances = 5.0F;
private float touchDelta = 0.0F;
private Vector2 prevDist = new Vector2(0,0);
private Vector2 curDist = new Vector2(0,0);
private float startAngleBetweenTouches = 0.0F;
private int vertOrHorzOrientation = 0; //this tells if the two fingers to each other are oriented horizontally or vertically, 1 for vertical and -1 for horizontal
private Vector2 midPoint = new Vector2(0,0); //store and use midpoint to check if fingers exceed a limit defined by midpoint for oriantation of fingers
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.touchCount == 2 Input.GetTouch(0).phase == TouchPhase.Began Input.GetTouch(1).phase == TouchPhase.Began)
{
}
if (Input.touchCount == 2 Input.GetTouch(0).phase == TouchPhase.Moved Input.GetTouch(1).phase == TouchPhase.Moved)
{
midPoint = new 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 ((Input.GetTouch(0).position.x - Input.GetTouch(1).position.x) > (Input.GetTouch(0).position.y - Input.GetTouch(1).position.y))
{
vertOrHorzOrientation = -1;
}
if ((Input.GetTouch(0).position.x - Input.GetTouch(1).position.x) < (Input.GetTouch(0).position.y - Input.GetTouch(1).position.y))
{
vertOrHorzOrientation = 1;
}
if ((touchDelta < 0)) //
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,90);
}
if ((touchDelta > 0))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);
}
}
}
}
Did you try this one? drag and drop the script on an object (it should work also on a camera).
function Update ()
{
if ( Input.touchCount == 2 ) {
var touch1 : iPhoneTouch = iPhoneInput.GetTouch( 0 );
var touch2 : iPhoneTouch = iPhoneInput.GetTouch( 1 );
// Find out how the touches have moved relative to eachother:
var curDist : Vector2 = touch1.position - touch2.position;
var prevDist : Vector2 = (touch1.position - touch1.positionDelta) - (touch2.position - touch2.positionDelta);
var touchDelta : float = curDist.magnitude - prevDist.magnitude;
// translate along local coordinate space
var MINSCALE : float = 1.5;
var MAXSCALE : float = 15;
Hey i am attaching this script to the camera but its not working…Not doing at all. Is there anything else apart from the script you have provided… Have tried quite a few scripts which work fine in other examples but not in my… Can there be a reason for it ? Have been banging head for quite sometime… Here is the script that i am attaching to the Main Camera.My camera is orthographic .
function Update ()
{
if ( Input.touchCount == 2 ) {
var touch1 : Touch = Input.GetTouch( 0 );
var touch2 : Touch = Input.GetTouch( 1 );
// Find out how the touches have moved relative to eachother:
var curDist : Vector2 = touch1.position - touch2.position;
var prevDist : Vector2 = (touch1.position - touch1.deltaPosition) - (touch2.position - touch2.deltaPosition);
var touchDelta : float = curDist.magnitude - prevDist.magnitude;
// translate along local coordinate space
var MINSCALE : float = 1.5;
var MAXSCALE : float = 15;
if ((transform.localScale.magnitude > MINSCALE)(touchDelta <= 1))
transform.localScale += Vector3(touchDelta*0.01,touchDelta*0.01,touchDelta *0.01 );
if ((transform.localScale.magnitude < MAXSCALE)(touchDelta > 1)) transform.localScale += Vector3(touchDelta*0.01,touchDelta*0.01,touchDelta *0.01 );
}
}
I got what you are doing …! you are scaling the object according to the distance between two touches…Don’t think it is right to play with the scale of game objects.One should do this with the camera. If there are 10+ objects on the scene this script has to be attached to each object so that every object would correspond to zoom.Rather if one works with camera only camera should be worked with and not other objects.This is what my thinking is .Correct me if I am wrong.!
The camera system expects 2 objects, all nested under each other. The bottom most nested object is the camera focus, the point on which the camera will rotate around when rotated. The top object (the parent with the camera) moves towards the focus or away for zooming, and also handles camera twists. The script expects to be attached to the top most object with the camera.
You may need to assign the camera component, or the camera focus gameobject, as I didn’t dynamically program them in this version of my script.