摄像机Demo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cameras : MonoBehaviour
{
public Transform target;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10;
public float yMinLimit = 5, yMaxLimit = 50;
public float distance = 50, minDistance = 2, maxDistance = 100;
public bool needDamping = true;
float damping = 5f;
public float x = 0f;
public float y = 0f;
//缩放
public Vector3 currentDirention;
private Vector3 TargetPoint;
private float targetDistance;
private bool keepMove;
public float pinchDist;
private float zoomVelocity;
//private Vector3 m_mouseMovePos;
private Camera cameras;
private void Start()
{
cameras = GetComponent<Camera>();
TargetPoint = target.position;
distance = Vector3.Distance(transform.position, TargetPoint);
targetDistance = distance;
}
// Update is called once per frame
void LateUpdate()
{
if (target)
{
//鼠标点击右键,围绕target旋转移动相机,改变视野
if (Input.touchCount == 1 /*|| Input.GetMouseButton(1)*/)
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
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;
}
}
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3(0f, 0f, -distance);
Vector3 position = rotation * disVector + target.position;
if (needDamping)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
}
}
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);
}
}
单指及双指手势识别Demo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SuoFang : MonoBehaviour
{
//存储手指触摸的数据
private Touch oldTouch1;
private Touch oldTouch2;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.touchCount <= 0)
{
return;
}
//当检测到多点时,放大缩小
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//当第二点刚接触屏幕时,只做记录,不做处理
if(newTouch2.phase == TouchPhase.Began)//.Phase描述触摸的阶段 .Began当一个手指触碰了屏幕的时候
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//计算老的两点之间的距离和新的两点之间的距离,距离变大放大模型,距离缩小缩小模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两者之间的距离之差
float offset = newDistance - oldDistance;
//放大因子,一个像素按0.1倍来计算(范围10)
float scaleFactor = offset / 10f;
Vector3 localScale = transform.localScale;
Vector3 scale = new Vector3(localScale.x + scaleFactor,
localScale.y + scaleFactor,
localScale.z + scaleFactor);
//声明方法执行条件
if(scale.x >= 0.05f && scale.y >= 0.05f && scale.z >= 0.05f)
{
transform.localScale = scale;
}
//记住新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}