I am targeting on 2d games lot more like clash of clans and created experimental project a remake of it. Problem is it doesn’t show the same camera view on mobile devices. Have a look at this image. I like a quick solution. For Max camera zoom.
Fixed
using UnityEngine;
using System.Collections;
public static class Extension
{
public static Bounds OrthographicBounds (this Camera camera)
{
if (!camera.orthographic)
{
Debug.Log(string.Format("The camera {0} is not Orthographic!", camera.name), camera);
return new Bounds();
}
var t = camera.transform;
var x = t.position.x;
var y = t.position.y;
var size = camera.orthographicSize * 2;
var width = size * (float)Screen.width / Screen.height;
var height = size;
return new Bounds(new Vector3(x, y, 0), new Vector3(width, height, 0));
}
//******Orthographic Camera Only******//
public static Vector2 BoundsMin(this Camera camera)
{
return (Vector2)camera.transform.position - camera.Extents();
}
public static Vector2 BoundsMax(this Camera camera)
{
return (Vector2)camera.transform.position + camera.Extents();
}
public static Vector2 Extents(this Camera camera)
{
if (camera.orthographic)
return new Vector2(camera.orthographicSize * Screen.width/Screen.height, camera.orthographicSize);
else
{
Debug.LogError("Camera is not orthographic!", camera);
return new Vector2();
}
}
//*****End of Orthographic Only*****//
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AdvancedCamera : MonoBehaviour {
public float PanSpeed = 0.05f;
public float PinchSpeed = 0.1f;
public float MinBoundX = -12.8112f; // Minimum Bondary From Center On Xaxis
public float MaxBoundX = 12.79478f; // Maximum Bondary From Center On Xaxis
public float MinBoundY = -10.80061f; // Minimum Bondary From Center On Yaxis
public float MaxBoundY = 9.691292f; // Maximum Bondary From Center On Yaxis
private float CamBoundX;
private float CamBoundY;
private float CamWorldMinX;
private float CamWorldMinY;
private float CamWorldMaxX;
private float CamWorldMaxY;
// Remove if not needed.
void Start(){
MinBoundX = -12.8112f;
MaxBoundX = 12.79478f;
MinBoundY = -10.80061f;
MaxBoundY = 9.691292f;
}
void Update(){
if (Camera.main.orthographicSize < 0) {
Camera.main.orthographicSize = Camera.main.orthographicSize * -1;
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 0, transform.rotation.w);
}
if (transform.rotation.z != 0)
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 0, transform.rotation.w);
CamBoundX = Camera.main.OrthographicBounds ().size.x / 2;
CamBoundY = Camera.main.OrthographicBounds ().size.y / 2;
CamWorldMinX = Camera.main.transform.position.x - CamBoundX;
CamWorldMinY = Camera.main.transform.position.y - CamBoundY;
CamWorldMaxX = Camera.main.transform.position.x + CamBoundX;
CamWorldMaxY = Camera.main.transform.position.y + CamBoundY;
var temporary = new Vector3 ();
if ((transform.position.x - CamBoundX) <= MinBoundX)
temporary.x = MinBoundX + CamBoundX;
if (transform.position.x == MinBoundX + CamBoundX && transform.position.x == MaxBoundX + CamBoundX)
if ((transform.position.y - CamBoundY) <= MinBoundY)
temporary.y = MinBoundY + CamBoundY;
if ((transform.position.x + CamBoundX) >= MaxBoundX)
temporary.x = MaxBoundX - CamBoundX;
if ((transform.position.y + CamBoundY) >= MaxBoundY)
temporary.y = MaxBoundY - CamBoundY;
if ((CamWorldMinX <= MinBoundX) && (CamWorldMaxX >= MaxBoundX))
temporary.x = 0f;
temporary.z = transform.position.z;
if ((transform.position.x - CamBoundX) <= MinBoundX || (transform.position.y - CamBoundY) <= MinBoundY || (transform.position.x + CamBoundX) >= MaxBoundX || (transform.position.y + CamBoundY) >= MaxBoundY)
transform.position = temporary;
if ((CamWorldMinX < MinBoundX) && (CamWorldMaxX > MaxBoundX))
Camera.main.orthographicSize -= (CamWorldMaxX - MaxBoundX);
}
// Update is called once per frame, of course.
void FixedUpdate () {
// Check if we have one finger down, and if it's moved.
// You may modify this first portion to '== 1', to only allow pinching or panning at one time.
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
var touchDeltaPosition = Input.GetTouch(0).deltaPosition;
var temporary = transform.position;
temporary -= new Vector3(touchDeltaPosition.x * PanSpeed, touchDeltaPosition.y * PanSpeed,0f);
if(((temporary.x - CamBoundX) >= MinBoundX) && ((temporary.x + CamBoundX) <= MaxBoundX) && ((temporary.y - CamBoundY) >= MinBoundY) && ((temporary.y + CamBoundY) <= MaxBoundY))
// Translate along world cordinates. (Done this way so we can angle the camera freely.)
transform.position = temporary;
}
// Check if we have two fingers down.
if ( Input.touchCount == 2 )
{
if (Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved) {
Touch touch1 = Input.GetTouch (0);
Touch touch2 = Input.GetTouch (1);
// Find out how the touches have moved relative to eachother.
Vector2 curDist = touch1.position - touch2.position;
Vector2 prevDist = (touch1.position - touch1.deltaPosition) - (touch2.position - touch2.deltaPosition);
float touchDelta = -(curDist.magnitude - prevDist.magnitude);
// Translate along local coordinate space.
var _value = Camera.main.orthographicSize;
_value += (touchDelta * PinchSpeed);
if (_value >= 0.9 && (_value + touchDelta * PinchSpeed) >= 0.9f)
if ((CamWorldMinX > MinBoundX) && (CamWorldMaxX < MaxBoundX)) {
Camera.main.orthographicSize = _value;
} else if(_value < Camera.main.orthographicSize){
Camera.main.orthographicSize = _value;
}
}
}
}
}
Thanks if anyone could simplify and remove bugs
Please share your solution and help future Users to solve such problem.
Thanks.
Hi! I developed a similar solution for this problem. You can check it here: GitHub - sergane13/Camera-Movement-By-Touch: Camera movement in Unity script, specific built for mobile devices
It is open source.

