Touch Orbit Pinch Zoom ( Please Help )

Hello Everyone,

After searching lots of about it, i couldn’t find a solution regarding my problem.

I have a target object, i just want to rotate the camera around it with max min distance and zoom the target object with pitching.

I have also a GUI area, which shouldn’t be affected by touch. ( I already solved this part and its in the code.)

Orbitting around target works fine. I actually use mouse input, but it works fine on iOs for now. So what i need as help is how to add a code to my script to have a pitch zoom with limitations.

I would be really happy if someone helps me on this. I’m trying to find this out since days…

Thank you all…:expressionless:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour {
	
	public Transform target;
	public float distance = 5.0f;
	public float xSpeed = 120.0f;
	public float ySpeed = 120.0f;
	
	public float yMinLimit = -20f;
	public float yMaxLimit = 80f;
	
	public float distanceMin = .5f;
	public float distanceMax = 15f;
	
	bool isGUI = false;
	
	float x = 0.0f;
	float y = 0.0f;
	
	void Start () {
		Vector3 angles = transform.eulerAngles;
		x = angles.y;
		y = angles.x;
		
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
	}

	void LateUpdate () {
		if (target  !isGUI) {
			x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
			y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
			
			y = ClampAngle(y, yMinLimit, yMaxLimit);
			
			Quaternion rotation = Quaternion.Euler(y, x, 0);
			
			distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*50, distanceMin, distanceMax);
			
			RaycastHit hit;
			if (Physics.Linecast (target.position, transform.position, out hit)) {
				distance -=  hit.distance;
			}
			Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
			Vector3 position = rotation * negDistance + target.position;
			
			transform.rotation = rotation;
			transform.position = position;
		}
	}
	
	public static float ClampAngle(float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp(angle, min, max);
	}

	
	 void OnGUI() {		
        GUILayout.BeginArea (new Rect(200, 420, 600, 350));
	GUILayout.Box("My button", GUILayout.Width(600),GUILayout.Height(350));

        if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition)) {
            Debug.Log("Mouse over!");
			isGUI=true;
		}
        else {
            Debug.Log("Mouse somewhere else");
		isGUI=false; }	
		GUILayout.EndArea();
    }
}

pretty much answers the question i think… couple of years old though.

Thank you “LeftyRighty”. The following page helps me lot about pitch zoom. And it works perfect. (: