2½D Tower Defense Game’s Touch Camera Controls

Updated on November 10, 2018 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on November 10, 2018

Hi,

I am developing a Tower Defense Themed Game and everything is working perfect except camera I need help for Fixing Bugs.

Btw My Camera Must Moving X and Z coordinates not X and Y so i try to screen x,y to world x,z too.

My Inspector Game View:

Inspector Camera Settings:

Play Mode Game View:

Play Mode Camera Settings:

And When I try to Pinch To Zoom Out in Play Mode ofc. orthographicSize decreasing too fast to almost -100000

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamAdvanced : MonoBehaviour {
public Camera cam;
public float PanSpeed = 0.05f;
public float PinchSpeed = 0.1f;
private float MinBoundX;
private float MaxBoundX;
private float MinBoundZ;
private float MaxBoundZ;
private float CamBoundX;
private float CamBoundZ;
private float CamWorldMinX;
private float CamWorldMinY;
private float CamWorldMaxX;
private float CamWorldMaxY;
public float maxZoom = 44f;
public float minZoom = 12f;
void Update()
{
CamBoundX = cam.transform.position.x /2f;
CamBoundZ = cam.transform.position.z /2f;
///////////// KEEP IN BOUND in Diffrent orthographicSize ////////////////
if (cam.orthographicSize >= 12f && cam.orthographicSize <= 16f)
{
MinBoundX = 18f;
MaxBoundX = 63f;
MinBoundZ = -10f;
MaxBoundZ = 59f;
}
else if (cam.orthographicSize >= 16f && cam.orthographicSize <= 20f)
{
MinBoundX = 25.5f;
MaxBoundX = 55.3f;
MinBoundZ = -4.5f;
MaxBoundZ = 54.5f;
}
else if (cam.orthographicSize >= 20f && cam.orthographicSize <= 24f)
{
MinBoundX = 33f;
MaxBoundX = 48f;
MinBoundZ = 1f;
MaxBoundZ = 48f;
}
else if (cam.orthographicSize >= 24f && cam.orthographicSize <= 28f)
{
MinBoundX = 40f;
MaxBoundX = 41f;
MinBoundZ = 7f;
MaxBoundZ = 42.5f;
}
else if (cam.orthographicSize >= 28f && cam.orthographicSize <= 32f)
{
MinBoundX = 33f;
MaxBoundX = 48;
MinBoundZ = 12f;
MaxBoundZ = 37f;
}
else if (cam.orthographicSize >= 32f && cam.orthographicSize <= 36f)
{
MinBoundX = 34f;
MaxBoundX = 47f;
MinBoundZ = 18f;
MaxBoundZ = 32f;
}
else if (cam.orthographicSize >= 36f && cam.orthographicSize <= 40f)
{
MinBoundX = 31f;
MaxBoundX = 51f;
MinBoundZ = 17f;
MaxBoundZ = 27f;
}
else if (cam.orthographicSize >= 40f && cam.orthographicSize <= 44f)
{
MinBoundX = 31f;
MaxBoundX = 51f;
MinBoundZ = 18f;
MaxBoundZ = 22f;
}
CamWorldMinX = cam.transform.position.x - CamBoundX;
CamWorldMinY = cam.transform.position.z - CamBoundZ;
CamWorldMaxX = cam.transform.position.x + CamBoundX;
CamWorldMaxY = cam.transform.position.z + CamBoundZ;
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.z - CamBoundZ) <= MinBoundZ)
temporary.z = MinBoundZ + CamBoundZ;
if ((transform.position.x + CamBoundX) >= MaxBoundX)
temporary.x = MaxBoundX - CamBoundX;
if ((transform.position.z + CamBoundZ) >= MaxBoundZ)
temporary.z = MaxBoundZ - CamBoundZ;
if ((CamWorldMinX <= MinBoundX) && (CamWorldMaxX >= MaxBoundX))
{
temporary.x = 40f;
temporary.y = 20f;
}
temporary.z = transform.position.z;
if ((transform.position.x - CamBoundX) <= MinBoundX || (transform.position.z - CamBoundZ) <= MinBoundZ || (transform.position.x + CamBoundX) >= MaxBoundX || (transform.position.z + CamBoundZ) >= MaxBoundZ)
transform.position = temporary;
if ((CamWorldMinX < MinBoundX) && (CamWorldMaxX > MaxBoundX))
cam.orthographicSize -= (CamWorldMaxX - MaxBoundX);
}
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 - CamBoundZ) >= MinBoundZ) && ((temporary.y + CamBoundZ) <= MaxBoundZ))
// 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 = cam.orthographicSize;
_value += (touchDelta * PinchSpeed);
if (_value >= 0.9 && (_value + touchDelta * PinchSpeed) >= 0.9f)
{
if ((CamWorldMinX > MinBoundX) && (CamWorldMaxX < MaxBoundX))
{
cam.orthographicSize = _value;
}
else if (_value < cam.orthographicSize)
{
cam.orthographicSize = _value;
}
}
float i = cam.orthographicSize + touchDelta * PinchSpeed;
if (i >= maxZoom)
{ //if i is greater than maxZoom, set camera to max
GetComponent<Camera>().orthographicSize = maxZoom;
}
else if (i <= minZoom)
{ //same but with min
GetComponent<Camera>().orthographicSize = minZoom;
}
else
{ //otherwise just update it to i
GetComponent<Camera>().orthographicSize = i;
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Extension 
{
public static Bounds OrthographicBounds(this Camera camera)
{
var t = camera.transform;
var x = t.position.x;
var z = t.position.z;
var size = camera.orthographicSize * 2;
var width = size * (float)Screen.width / (float)Screen.height;
var height = size;
return new Bounds(new Vector3(x, 20, z), new Vector3(width, 20, height));
}
}
  • Liked by
Reply