Problem with pinch zoom

This script is attached to a GUITexture.
I need to scale it with the standard pinch/spread.
I can’t understand what is wrong

#pragma strict



private var distBetweenTouches : float; 
private var newDist  : float;
private var twoTouches: boolean = false; 
private var scaleFactor: float = 1; 
private var pos1 : Vector2;
private var pos2 : Vector2;
private var newPos1 : Vector2;
private var newPos2 : Vector2;


function OnGUI() {  
	
	if ((iPhoneInput.touchCount == 2)  !twoTouches) { 
             pos1  = iPhoneInput.touches[0].position; 
             pos2  = iPhoneInput.touches[1].position; 
             twoTouches = true;

             distBetweenTouches = Vector2.Distance( pos1, pos2 );
                           
            }     
} 

function Update(){
				 	
			if (twoTouches) 
   			{ 	
   	    		newPos1  = iPhoneInput.touches[0].position; 
       			newPos2  = iPhoneInput.touches[1].position; 
        	
        		newDist = Vector2.Distance( newPos1, newPos2 ); 
              
       			scaleFactor = newDist / distBetweenTouches; 
              
       			transform.localScale = Vector3  (scaleFactor,scaleFactor,0);
    		} 
    			twoTouches = false;        
}

Probably because of the order of execution. I’d imagine that Update was called before OnGUI, so the way your code flows wouldn’t work.

In this instance, I wouldn’t break out your code into multiple functions - it’s pretty sloppy! Try re-writing it so everything is done in Update() - the logic behind using OnGUI to do this escapes me, if I’m honest…

I think this post should help you out:
http://forum.unity3d.com/viewtopic.php?p=112566#112566

Thanks,
now it is better, but the problem is to make the GUITexture always centered in relation to the fingers.

#pragma strict



public var scaleFactor: float = 10; 
public var maxZoom : float = 20; 

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 ) {
          transform.localScale.x -= ( touch1.deltaPosition.x - touch2.deltaPosition.x ) / scaleFactor; 
          transform.localScale.y -= ( touch1.deltaPosition.x - touch2.deltaPosition.x ) / scaleFactor;
 
      }
      if ( touch1.position.x > touch2.position.x ) {
         transform.localScale.x += ( touch1.deltaPosition.x - touch2.deltaPosition.x ) / scaleFactor; 
         transform.localScale.y += ( touch1.deltaPosition.x - touch2.deltaPosition.x ) / scaleFactor; 
      }
      
      if(transform.localScale.x >= maxZoom)
      	 transform.localScale.x = maxZoom;
      if(transform.localScale.y >= maxZoom)
      	 transform.localScale.y = maxZoom;
      if(transform.localScale.x <= 1)
      	 transform.localScale.x = 1;
      if(transform.localScale.y <= 1)
      	 transform.localScale.y = 1;
	} 
    			  
    			    
}

If you mean you want it to zoom in on the area between the fingers, then that’s going to be a little more complex. Basically, you’ll need to determine the total change in size (X and Y) and then offset it based on where the center point of the pinch is taking place.

So if the pinch happens in the center, then offset is 0.

If it takes place at the far right edge, then it should be offset toward the left by half the size change value.

Etc.

I think that should work. The trick, of course, is translating that into code. :slight_smile: