0% found this document useful (0 votes)
176 views2 pages

Simple Touch Camera Script For Unity

This script allows touch camera control in Unity without any additional configuration. It handles single and dual touch input to move and rotate the camera. For single touch, it moves the camera based on the touch position. For dual touch, it supports pinch to zoom by adjusting the camera size based on the distance between touches, and rotate by calculating the angle between current and previous touch vectors.

Uploaded by

marius_alex_32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views2 pages

Simple Touch Camera Script For Unity

This script allows touch camera control in Unity without any additional configuration. It handles single and dual touch input to move and rotate the camera. For single touch, it moves the camera based on the touch position. For dual touch, it supports pinch to zoom by adjusting the camera size based on the distance between touches, and rotate by calculating the angle between current and previous touch vectors.

Uploaded by

marius_alex_32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Just add this script to your camera. It doesn't need any configuration.

using UnityEngine;
public class TouchCamera : MonoBehaviour {
Vector2?[] oldTouchPositions = {
null,
null
};
Vector2 oldTouchVector;
float oldTouchDistance;
void Update() {
if (Input.touchCount == 0) {
oldTouchPositions[0] = null;
oldTouchPositions[1] = null;
}
else if (Input.touchCount == 1) {
if (oldTouchPositions[0] == null || oldTouchPositions[1]
!= null) {
oldTouchPositions[0] = Input.GetTouch(0).positio
n;
oldTouchPositions[1] = null;
}
else {
Vector2 newTouchPosition = Input.GetTouch(0).pos
ition;
transform.position += transform.TransformDirecti
on((Vector3)((oldTouchPositions[0] - newTouchPosition) * camera.orthographicSize
/ camera.pixelHeight * 2f));
oldTouchPositions[0] = newTouchPosition;
}
}
else {
if (oldTouchPositions[1] == null) {
oldTouchPositions[0] = Input.GetTouch(0).positio
n;
oldTouchPositions[1] = Input.GetTouch(1).positio
n;
oldTouchVector = (Vector2)(oldTouchPositions[0]
- oldTouchPositions[1]);
oldTouchDistance = oldTouchVector.magnitude;
}
else {
Vector2 screen = new Vector2(camera.pixelWidth,
camera.pixelHeight);
Vector2[] newTouchPositions = {
Input.GetTouch(0).position,
Input.GetTouch(1).position
};
Vector2 newTouchVector = newTouchPositions[0] newTouchPositions[1];
float newTouchDistance = newTouchVector.magnitud
e;
transform.position += transform.TransformDirecti
on((Vector3)((oldTouchPositions[0] + oldTouchPositions[1] - screen) * camera.ort

hographicSize / screen.y));
transform.localRotation *= Quaternion.Euler(new
Vector3(0, 0, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldT
ouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)
) / 0.0174532924f));
camera.orthographicSize *= oldTouchDistance / ne
wTouchDistance;
transform.position -= transform.TransformDirecti
on((newTouchPositions[0] + newTouchPositions[1] - screen) * camera.orthographicS
ize / screen.y);
oldTouchPositions[0] = newTouchPositions[0];
oldTouchPositions[1] = newTouchPositions[1];
oldTouchVector = newTouchVector;
oldTouchDistance = newTouchDistance;
}
}
}
}

You might also like