- Home /
Question by
Qhailashnikov · Dec 16, 2018 at 10:03 AM ·
cameramovementzoommobile devices
How to add Pinch to Zoom?
Hey guys, I'm having problem figuring out the Pinch to Zoom. I tried to find on Google and Youtube and did not find one that works.
so here is the Script, I was wondering if you guys add it for me or maybe translate the zoom with mouse wheel with two touch fingers code. Thanks. Apology for not so good english I was stressed out trying to figure this out haha
using UnityEngine;
public class CameraController : MonoBehaviour {
public float panSpeed = 30f;
public float panSpeedMobile;
public float panBorderThickness = 10f;
public float scrollSpeed = 5f;
public float minY = 10f;
public float maxY = 80f;
public float zoomRate = 10.0f;
public float zoomDampening = 5.0f;
private float desiredDistance;
// Update is called once per frame
void Update () {
if(GameManager.GameIsOver)
{
this.enabled = false;
return;
}
if (Input.GetKey("w") || Input.mousePosition.y >= Screen.height - panBorderThickness)
{
transform.Translate(Vector3.forward * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
{
transform.Translate(Vector3.back * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - panBorderThickness)
{
transform.Translate(Vector3.right * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness)
{
transform.Translate(Vector3.left * panSpeed * Time.deltaTime, Space.World);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * panSpeedMobile * Time.deltaTime, -touchDeltaPosition.y * panSpeedMobile * Time.deltaTime, 0);
}
float scroll = Input.GetAxis("Mouse ScrollWheel");
Vector3 pos = transform.position;
pos.y -= scroll * 1000 * scrollSpeed * Time.deltaTime;
pos.y = Mathf.Clamp(pos.y, minY, maxY);
transform.position = pos;
}
}
Comment
Your answer
Follow this Question
Related Questions
Different Camera changes work against each other? 1 Answer
Camera movement and zoom bounds 0 Answers
How to make a camera zoom on a particular part of image plane? 1 Answer
Camera and Mouse Movement 0 Answers
How do you differentiate between pinch to zoom with two fingers and a two finger drag? 2 Answers