unity控制相機實現旋轉縮放——觸屏版(單指控制旋轉,結束有慣性滑動,雙指控制縮放,根據手指間距離變化程度控制縮放程度)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public float pinchDist;
private float zoomVelocity;
private float targetDistance;
private float minDistance = 3;
private float maxDistance = 8;
private float distance;
public Transform car;
private Vector3 TargetPoint;
float x;
float y;
public float speed = 1f;
float xSpeed = 30f;
float ySpeed = 30f;
public float angle;
public float targetangle;
public int yMinLimit = 40;
public int yMaxLimit = 85;
private float m_touchMaxDist = 20f;
public Vector3 currentDirention;
private bool keepMove;
private float waittime = 3f;
private bool m_autoRotation;
private float smoothness = 0.16f;
private readonly Vector2 m_fitScreenRes = new Vector2(2048f, 1536f);
float roatspeedy;
private void Awake()
{
currentDirention = transform.position - TargetPoint;
angle = Vector3.Angle(currentDirention, new Vector3(0, 1, 0));
targetangle = angle;
float xFactor = m_fitScreenRes.x / Screen.width;
Debug.Log(xFactor);
float yFactor = m_fitScreenRes.y / Screen.height;
Debug.Log(yFactor);
xSpeed = xFactor * speed;
ySpeed = yFactor * speed;
m_touchMaxDist /= xFactor;
}
// Use this for initialization
void Start()
{
TargetPoint = car.position;
distance = Vector3.Distance(transform.position, TargetPoint);
targetDistance = distance;
}
// Update is called once per frame
void Update()
{
float time = Time.deltaTime;
if (time > 0.035f)
{
time = 0.035f;
}
if (keepMove) //慣性(緩動)
{
if (x > -0.1f && x < 0.1f)
{
keepMove = false;
}
// Debug.Log ("慣性: " + x);
x = x - x * smoothness;
//x=Mathf.Lerp(x, 0f, 3f);
//y = y - y * smoothness;
transform.RotateAround(TargetPoint, Vector3.up, x * xSpeed * time*0.2f);
//return;
//transform.RotateAround(TargetPoint, transform.right, -y * ySpeed * Time.deltaTime);
}
//自動旋轉
//if (m_autoRotation)
//{
// transform.RotateAround(TargetPoint, Vector3.up, -0.1f);
//}
//else
//{
// waittime -= Time.deltaTime;
// if (waittime <= 0)
// {
// waittime = 3;
// m_autoRotation = true;
// }
//}
targetDistance = Vector3.Distance(transform.position, TargetPoint);
if (Input.touchCount == 1)
{
keepMove = false;
if(Input.touches[0].phase == TouchPhase.Moved)
{
x = Input.touches[0].deltaPosition.x;
y = Input.touches[0].deltaPosition.y;
x = Mathf.Clamp(x, -m_touchMaxDist, m_touchMaxDist);
y = Mathf.Clamp(y, -m_touchMaxDist, m_touchMaxDist);
transform.RotateAround(TargetPoint, Vector3.up, x * xSpeed * time*0.1f);
currentDirention = transform.position - TargetPoint;
roatspeedy = y * ySpeed * time;
targetangle = angle + roatspeedy*0.2f;
if (targetangle < yMinLimit)
{
//roatspeedy=speed*Time.deltaTime;
roatspeedy = 0;
targetangle = yMinLimit;
}
if (targetangle > yMaxLimit)
{
//roatspeedy=-speed*Time.deltaTime;
roatspeedy = 0;
targetangle = yMaxLimit;
}
transform.RotateAround(TargetPoint, transform.right, -roatspeedy*0.2f);
currentDirention = transform.position - TargetPoint;
angle = Vector3.Angle(currentDirention, Vector3.up); //求出兩向量之間的夾角
}
else if(Input.touches[0].phase == TouchPhase.Ended)
{
keepMove = true;
}
}
if (Input.touches[1].phase == TouchPhase.Began)
{
pinchDist = 0;
}
if (Input.touchCount == 2)
{
keepMove = false;
if (Input.touches[0].phase == TouchPhase.Moved || Input.touches[1].phase == TouchPhase.Moved)
{
if (pinchDist == 0)
{
pinchDist = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
}
else
{
float dx = ((pinchDist - (float)Vector2.Distance(Input.touches[0].position, Input.touches[1].position)) * 0.04f);
zoomVelocity = dx;
if (targetDistance + zoomVelocity < minDistance)
{
zoomVelocity = minDistance - targetDistance;
}
else if (targetDistance + zoomVelocity > maxDistance)
{
zoomVelocity = maxDistance - targetDistance;
}
targetDistance += zoomVelocity;
pinchDist = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
}
currentDirention = transform.position - TargetPoint;
distance = Mathf.Lerp(distance, targetDistance, 0.3f);
transform.position = TargetPoint + distance * currentDirention.normalized;
//transform.LookAt(TargetPoint);
}
}
}
}