- Home /
Question by
hashtagz28 · Dec 11, 2018 at 12:10 PM ·
camerazoompanning
panning & zooming using multitouch
hi, I am the problem i am running into is that i need to pan the camera and zoom in and other using two touch's as my input the problem is when i move my fingers to pan it also results in zooming in i have tried using a dot product of the two touch's but cant come to a solution.Also i am using one into to rotate the object on the screen thank u for the answer in advance.
Comment
I forgot to add I was trying a package to get what i wanted
Answer by zohaibzaidi · Dec 11, 2018 at 02:25 PM
This is a script I used a couple of times.. Should fix your problem.. You can remove the HasReachedBounds method. The code might need some modification. Usually works straightforward for me.
// 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;
private Camera camera;
public SpriteRenderer mapSprite;
Vector3 spriteMin;
Vector3 spriteMax;
private void Start()
{
camera = GetComponent<Camera>();
Bounds spriteBounds = mapSprite.bounds;
spriteMin = spriteBounds.min;
spriteMax = spriteBounds.max;
}
enum BoundReached
{
left,
right,
top,
bottom
}
BoundReached boundReached = BoundReached.left;
void Update()
{
if (HasReachedBounds())
{
//lerp back
if (Input.touchCount == 0)
{
var vertExtent = camera.orthographicSize;
var horzExtent = vertExtent * Screen.width / Screen.height;
if (boundReached == BoundReached.left)
{
transform.position = Vector3.Lerp(transform.position, new Vector3(spriteMin.x + horzExtent + 1, transform.position.y, transform.position.z), Time.deltaTime * 5);
}
else if (boundReached == BoundReached.right)
{
transform.position = Vector3.Lerp(transform.position, new Vector3(spriteMax.x - horzExtent - 1, transform.position.y, transform.position.z), Time.deltaTime * 5);
}
else if (boundReached == BoundReached.top)
{
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, spriteMax.y - vertExtent - 1, transform.position.z), Time.deltaTime * 5);
}
else if (boundReached == BoundReached.bottom)
{
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, spriteMin.y + vertExtent + 1, transform.position.z), Time.deltaTime * 5);
}
}
}
else
{
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).position;
oldTouchPositions[1] = null;
}
else
{
Vector2 newTouchPosition = Input.GetTouch(0).position;
transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] - newTouchPosition) * camera.orthographicSize / camera.pixelHeight * 2f));
oldTouchPositions[0] = newTouchPosition;
}
}
else
{
if (oldTouchPositions[1] == null)
{
oldTouchPositions[0] = Input.GetTouch(0).position;
oldTouchPositions[1] = Input.GetTouch(1).position;
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.magnitude;
transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] + oldTouchPositions[1] - screen) * camera.orthographicSize / screen.y));
//transform.localRotation *= Quaternion.Euler(new Vector3(0, 0, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f));
camera.orthographicSize *= oldTouchDistance / newTouchDistance;
camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, 5, 40);
transform.position -= transform.TransformDirection((newTouchPositions[0] + newTouchPositions[1] - screen) * camera.orthographicSize / screen.y);
oldTouchPositions[0] = newTouchPositions[0];
oldTouchPositions[1] = newTouchPositions[1];
oldTouchVector = newTouchVector;
oldTouchDistance = newTouchDistance;
}
}
}
if (HasReachedBounds())
{
Debug.Log("has reached left sprite bounds");
}
}
bool HasReachedBounds()
{
var vertExtent = camera.orthographicSize;
var horzExtent = vertExtent * Screen.width / Screen.height;
if (transform.position.x - horzExtent < spriteMin.x)
{
boundReached = BoundReached.left;
return true;
}
else if (transform.position.x + horzExtent > spriteMax.x)
{
boundReached = BoundReached.right;
return true;
}
else if (transform.position.y - vertExtent < spriteMin.y)
{
boundReached = BoundReached.bottom;
return true;
}
else if (transform.position.y + vertExtent > spriteMax.y)
{
boundReached = BoundReached.top;
return true;
}
else
{
return false;
}
}
}