Repository navigation
Description
Hi Im new in GitHub, and I found this code a few years ago from these pages:
http://touchprefab.com/search.php?keywords=Camera+Zoom
http://touchprefab.com/viewtopic.php?f=4&t=24272&p=26743&hilit=Camera+Zoom#p26743
Thanks to nmax
I did some modifications to make it run back in the day. But now when I update TouchScript to the newest version, the code gets broken, I tried to repair it but without success :(
I only want the camera moves softly to the planet (Im using the Camera example) with a limit for the camera to not cross the planet and not to walk away without limits.
Any idea how to repair it?
Thanks for your help :)
//original
using UnityEngine;
using System.Collections;
using System;
using TouchScript;
using TouchScript.Gestures;
public class SuperCamera : MonoBehaviour
{
public Transform target;
public Vector3 targetOffset;
public float distance = 5.0f;
public float maxDistance = 20;
public float minDistance = 5f;
public float xSpeed = 15f;
public float ySpeed = 15f;
public int yMinLimit = -90;
public int yMaxLimit = 90;
public int zoomRate = 50;
public float panSpeed = 10f;
public float zoomDampening = 3.0f;
public bool Enabled = true;
public bool ClampOrbit = true;
//------------------------------------------------------------
private float xDeg = 0.0f;
private float yDeg = 0.0f;
private float currentDistance;
private float desiredDistance;
private Quaternion currentRotation;
private Quaternion desiredRotation;
private Quaternion rotation;
private Vector3 position;
private Vector3 touchDeltaPosition;
private float Dummy;
void Start()
{
Init();
}
public void Init()
{
//If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
if (!target)
{
GameObject go = new GameObject("Cam Target");
go.transform.position = new Vector3(0,0,0);//transform.position + (transform.forward * distance);
target = go.transform;
}
distance = Vector3.Distance(transform.position, target.position);
currentDistance = distance;
desiredDistance = distance;
//be sure to grab the current rotations as starting points.
position = transform.position;
rotation = transform.rotation;
currentRotation = transform.rotation;
desiredRotation = transform.rotation;
xDeg = Vector3.Angle(Vector3.right, transform.right);
yDeg = Vector3.Angle(Vector3.up, transform.up);
Dummy = distance;
}
private void OnEnable()
{
if (GetComponent<TransformGesture>() != null)
{
GetComponent<TransformGesture>().StateChanged += onPanStateChanged;
}
if (GetComponent<TransformGesture>() != null)
{
GetComponent<TransformGesture>().StateChanged += onScaleStateChanged;
}
if (GetComponents<TapGesture>() != null)
{
foreach (var tap in GetComponents<TapGesture>())
{
tap.Tapped += tappedHandler;
}
}
if (GetComponent<LongPressGesture>() != null)
{
GetComponent<LongPressGesture>().StateChanged += longPressStateChangedHandler;
}
}
private void OnDisable()
{
if (GetComponent<TransformGesture>() != null)
{
GetComponent<TransformGesture>().StateChanged -= onPanStateChanged;
}
if (GetComponent<TransformGesture>() != null)
{
GetComponent<TransformGesture>().StateChanged -= onScaleStateChanged;
}
if (GetComponents<TapGesture>() != null)
{
foreach (var tap in GetComponents<TapGesture>())
{
tap.Tapped -= tappedHandler;
}
}
if (GetComponent<LongPressGesture>() != null)
{
GetComponent<LongPressGesture>().StateChanged -= longPressStateChangedHandler;
}
}
private void onPanStateChanged(object sender, GestureStateChangeEventArgs e)
{
if (!Enabled)
{
return;
}
switch (e.State)
{
case Gesture.GestureState.Began:
case Gesture.GestureState.Changed:
var gesture = (TransformGesture)sender;
touchDeltaPosition = gesture.LocalDeltaPosition;
if (gesture.ActiveTouches.Count != 1)
{
return;
}
xDeg += touchDeltaPosition.x * xSpeed * 0.02f;
yDeg -= touchDeltaPosition.y * ySpeed * 0.02f;
break;
}
}
private void onScaleStateChanged(object sender, GestureStateChangeEventArgs e)
{
if (!Enabled)
{
return;
}
switch (e.State)
{
case Gesture.GestureState.Began:
case Gesture.GestureState.Changed:
var gesture = (TransformGesture)sender;
if (Math.Abs(gesture.DeltaScale - 1) > 0.00001) //LocalDeltaScale => DeltaScale
{
float speed = (gesture.DeltaScale - 1);
distance = speed;
if (distance > maxDistance)
{
desiredDistance += distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
if (distance < minDistance)
{
desiredDistance -= distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
}
break;
}
}
private void tappedHandler(object sender, EventArgs eventArgs)
{
if (!Enabled)
{
return;
}
var tap = sender as TapGesture;
switch (tap.NumberOfTapsRequired)
{
case 1:
// If you want to have ONE TAP
break;
case 2:
// DOUBLE TAP ZOOM OUT & IN
desiredDistance = Dummy;
if (distance > maxDistance)
{
desiredDistance += distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
if (distance < minDistance)
{
desiredDistance -= distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
break;
}
}
private void longPressStateChangedHandler(object sender, GestureStateChangeEventArgs e)
{
if (!Enabled)
{
return;
}
switch (e.State)
{
case Gesture.GestureState.Recognized:
case Gesture.GestureState.Failed:
case Gesture.GestureState.Cancelled:
//Do your action if CANCEL LONG PRESS
break;
}
if (e.State == Gesture.GestureState.Recognized)
{
// Do your action on LONG PRESS
}
}
void LateUpdate()
{
if (ClampOrbit)
{
//Clamp the vertical axis for the orbit
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
}
// set camera rotation
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
transform.rotation = rotation;
////////Orbit Position
//clamp the zoom min/max
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
// For smoothing of the zoom, lerp distance
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
//calculate position based on the new currentDistance
if (target == null)
{
return;
}
if (target)
{
position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset); //MOD
}
position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset); //MOD
transform.position = position;
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
//My repair but with errors :( (it happens when Im trying to use 2 fingers for scaling)
//transform.localPosition assign attempt for 'Camera' is not valid. Input localPosition is { NaN, NaN, Infinity }.
UnityEngine.Transform:set_localPosition(Vector3)
using UnityEngine;
using System.Collections;
using System;
using TouchScript;
using TouchScript.Gestures;
public class CameraDif01 : MonoBehaviour
{
public Transform target;
public Vector3 targetOffset;
public float distance = 5.0f;
public float maxDistance = 20;
public float minDistance = 5f;
public float xSpeed = 15f;
public float ySpeed = 15f;
public int yMinLimit = -90;
public int yMaxLimit = 90;
public int zoomRate = 50;
public float panSpeed = 10f;
public float zoomDampening = 3.0f;
public bool Enabled = true;
public bool ClampOrbit = true;
//------------------------------------------------------------
private float xDeg = 0.0f;
private float yDeg = 0.0f;
private float currentDistance;
private float desiredDistance;
private Quaternion currentRotation;
private Quaternion desiredRotation;
private Quaternion rotation;
private Vector3 position;
private Vector3 touchDeltaPosition;
private float Dummy;
void Start()
{
Init();
}
public void Init()
{
//If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
if (!target)
{
GameObject go = new GameObject("Cam Target");
go.transform.position = new Vector3(0,0,0);//transform.position + (transform.forward * distance);
target = go.transform;
}
distance = Vector3.Distance(transform.position, target.position);
currentDistance = distance;
desiredDistance = distance;
//be sure to grab the current rotations as starting points.
position = transform.position;
rotation = transform.rotation;
currentRotation = transform.rotation;
desiredRotation = transform.rotation;
xDeg = Vector3.Angle(Vector3.right, transform.right);
yDeg = Vector3.Angle(Vector3.up, transform.up);
Dummy = distance;
}
private void OnEnable()
{
if (GetComponent<TransformGestureBase>() != null) //TransformGesture
{
GetComponent<TransformGestureBase>().StateChanged += onPanStateChanged; //TransformGesture
}
if (GetComponent<TransformGestureBase>() != null) //TransformGesture
{
GetComponent<TransformGestureBase>().StateChanged += onScaleStateChanged; //TransformGesture
}
if (GetComponents<TapGesture>() != null)
{
foreach (var tap in GetComponents<TapGesture>())
{
tap.Tapped += tappedHandler;
}
}
if (GetComponent<LongPressGesture>() != null)
{
GetComponent<LongPressGesture>().StateChanged += longPressStateChangedHandler;
}
}
private void OnDisable()
{
if (GetComponent<TransformGestureBase>() != null) //TransformGesture
{
GetComponent<TransformGestureBase>().StateChanged -= onPanStateChanged; //TransformGesture
}
if (GetComponent<TransformGestureBase>() != null) //TransformGesture
{
GetComponent<TransformGestureBase>().StateChanged -= onScaleStateChanged; //TransformGesture
}
if (GetComponents<TapGesture>() != null)
{
foreach (var tap in GetComponents<TapGesture>())
{
tap.Tapped -= tappedHandler;
}
}
if (GetComponent<LongPressGesture>() != null)
{
GetComponent<LongPressGesture>().StateChanged -= longPressStateChangedHandler;
}
}
private void onPanStateChanged(object sender, GestureStateChangeEventArgs e)
{
if (!Enabled)
{
return;
}
switch (e.State)
{
case Gesture.GestureState.Began:
case Gesture.GestureState.Changed:
var gesture = (TransformGestureBase)sender; //TransformGesture
touchDeltaPosition = gesture.DeltaPosition; //LocalDeltaPosition
if (gesture.ActivePointers.Count != 1) //ActiveTouches
{
return;
}
xDeg += touchDeltaPosition.x * xSpeed * 0.02f;
yDeg -= touchDeltaPosition.y * ySpeed * 0.02f;
break;
}
}
private void onScaleStateChanged(object sender, GestureStateChangeEventArgs e)
{
if (!Enabled)
{
return;
}
switch (e.State)
{
case Gesture.GestureState.Began:
case Gesture.GestureState.Changed:
var gesture = (TransformGestureBase)sender; //TransformGesture
if (Math.Abs(gesture.DeltaScale - 1) > 0.00001) //LocalDeltaScale => DeltaScale
{
float speed = (gesture.DeltaScale - 1);
distance = speed;
if (distance > maxDistance)
{
desiredDistance += distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
if (distance < minDistance)
{
desiredDistance -= distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
}
break;
}
}
private void tappedHandler(object sender, EventArgs eventArgs)
{
if (!Enabled)
{
return;
}
var tap = sender as TapGesture;
switch (tap.NumberOfTapsRequired)
{
case 1:
// If you want to have ONE TAP
break;
case 2:
// DOUBLE TAP ZOOM OUT & IN
desiredDistance = Dummy;
if (distance > maxDistance)
{
desiredDistance += distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
if (distance < minDistance)
{
desiredDistance -= distance * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
}
break;
}
}
private void longPressStateChangedHandler(object sender, GestureStateChangeEventArgs e)
{
if (!Enabled)
{
return;
}
switch (e.State)
{
case Gesture.GestureState.Recognized:
case Gesture.GestureState.Failed:
case Gesture.GestureState.Cancelled:
//Do your action if CANCEL LONG PRESS
break;
}
if (e.State == Gesture.GestureState.Recognized)
{
// Do your action on LONG PRESS
}
}
void LateUpdate()
{
if (ClampOrbit)
{
//Clamp the vertical axis for the orbit
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
}
// set camera rotation
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
transform.rotation = rotation;
////////Orbit Position
//clamp the zoom min/max
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
// For smoothing of the zoom, lerp distance
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
//calculate position based on the new currentDistance
if (target == null)
{
return;
}
if (target)
{
position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset); //MOD
}
position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset); //MOD
transform.position = position;
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
Activity