- Home /
Camera with inertia and zoom
Hello to everyone. The code bellow is a camera script with inertia and zoom. It works perfectly. However when the script is running i cant do other camera transforms like slerp. Plz share your thoughts and examples.
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour
{
private Vector3 firstpoint;
private Vector3 secondpoint;
private float xAngle= 0.0f;
private float yAngle = 0.0f;
private float xAngTemp = 0.0f;
private float yAngTemp = 0.0f;
private float targetAngleX = 0.0f;
private float currentAngleX = 0.0f;
private float targetAngleY = 0.0f;
private float currentAngleY = 0.0f;
public float perspectiveZoomSpeed = 0.1f;
void Start()
{
currentAngleY = 0.0f;
currentAngleX = 0.0f;
this.transform.rotation = Quaternion.Euler(currentAngleY, currentAngleX, 0.0f);
}
void Update()
{
float xPos=Input.GetAxis("Mouse X");
float yPos=Input.GetAxis("Mouse Y");
if (Input.touchCount > 0) {
if (Input.touchCount == 1) {
xPos = Input.touches [0].deltaPosition.x;
yPos = Input.touches [0].deltaPosition.y;
if (Input.GetTouch (0).phase == TouchPhase.Began) {
firstpoint = Input.GetTouch (0).position;
xAngTemp = xAngle;
yAngTemp = yAngle;
}
if (Input.GetTouch (0).phase == TouchPhase.Moved) {
secondpoint = Input.GetTouch (0).position;
yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;
targetAngleY += -yPos * 0.1f;
if (targetAngleY < currentAngleY) {
while (currentAngleY - targetAngleY > 360)
targetAngleY += 360;
} else if (targetAngleY > currentAngleY) {
while (targetAngleY - currentAngleY > 360)
targetAngleY -= 360;
}
xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
targetAngleX += xPos * 0.1f;
if (targetAngleX < currentAngleX) {
while (currentAngleX - targetAngleX > 360)
targetAngleX += 360;
} else if (targetAngleX > currentAngleX) {
while (targetAngleX - currentAngleX > 360)
targetAngleX -= 360;
}
}
} else if (Input.touchCount == 2) {
Zooming ();
}
}
currentAngleX += Time.deltaTime * ((targetAngleX - currentAngleX) * 3);
if (Mathf.Abs (currentAngleX - targetAngleX) < 1)
targetAngleX = currentAngleX;
currentAngleY += Time.deltaTime * ((targetAngleY - currentAngleY) * 3);
if (Mathf.Abs (currentAngleY - targetAngleY) < 1)
targetAngleY = currentAngleY;
transform.rotation = Quaternion.Euler (currentAngleY, currentAngleX, 0.0f);
}
void Zooming()
{
Touch touchZero = Input.GetTouch (0);
Touch touchOne = Input.GetTouch (1);
if (Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (1).phase == TouchPhase.Moved)
{
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
Camera.main.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
Camera.main.fieldOfView = Mathf.Clamp (Camera.main.fieldOfView, 40.0f, 65.0f);
}
}
}
Comment