Pinch code

I got the following code from the forum, it allows the user to pinch (on iphone), but im having a few troubles, how can I add the delta to the cameras z pos, so it zooms in and out?

using UnityEngine;
using System.Collections;

public class Pinch : MonoBehaviour {




	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		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; 

Debug.Log(delta);

transform.Translate.z = delta;

	
	}
}

create a js script and past this in it:

function Update () {

   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;

// To add limits to the camra movement
      
      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;

  }

then drop it on your MainCamera and bingo.

From this forum:

http://forum.unity3d.com/viewtopic.php?t=16610&highlight=pinch

Enjoy!

Robert

I lean towards the “teach a man to fish” philosophy, so I’m going to say you need to hit the docs. Moving the camera is a pretty basic Unity concept.

However, so that you’re not left with nothing, try replacing “transform.Translate.z = delta” with

Camera.main.transform.Translate(0, 0, delta);

and you’ll be set.