摄像机的旋转缩放与跟随,并且保持跟随的时候不受人物的影响。

using UnityEngine;
using System.Collections;

public class MainFinger : MonoBehaviour {
    /// <summary>
    /// 用于绑定参照物体
    /// </summary>
    public Transform target;
    /// <summary>
    /// 缩放系数
    /// </summary>
    public float distance=10;
    /// <summary>
    /// 左右滑动移动速度
    /// </summary>
    public float xSpeed = 250;
    public float ySpeed = 120;
    /// <summary>
    /// 缩放限制系数
    /// </summary>
    public float yMinLimit = -20;
    public float yMaxLimit = 80;
    /// <summary>
    /// 摄像头的位置
    /// </summary>
    public float x = 0;
    public float y = 0;
    /// <summary>
    /// 记录上一次手机触摸位置判断用户是在放大还是在缩小。
    /// </summary>
    private Vector2 oldPosition1;
    private Vector2 oldPosition2;
    private string shang;
    /// <summary>
    /// 鼠标中轴的放大缩小
    /// </summary>
    public float minDistance = 2;
    public float maxDistance = 10;
    public float mSpeed = 6;
	// Use this for initialization
	void Start () {
        var angles = transform.eulerAngles;
        x = angles.x;
        y = angles.y;
	}
	
	// Update is called once per frame
	void Update () {
        //判断触摸的数量为单点触摸。
        distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
        Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
        distance = Mathf.Clamp(distance, minDistance, maxDistance);
        if (Input.touchCount==1||Input.GetMouseButton(0))
        {
            //触摸模型为移动触摸
            if (Input.GetMouseButton(0))
            {
                //根据触摸点计算x与y的位置
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y += Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            }
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                //根据触摸点计算x与y的位置
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y += Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            }
        }
        //判断触摸数量为多点触摸
        if (Input.touchCount>1)
        {
            shang = "双指";
            //前两只手指触摸都为移动触摸
            if (Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved)
            {
                shang = "爽直啊啊";
                //计算出当前两点触摸点的位置
                Vector2 tempPosition1 = Input.GetTouch(0).position;
                Vector2 tempPosition2 = Input.GetTouch(1).position;
                //返回真为放大,返回假为缩小
                if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                {
                    if (Camera.main.fieldOfView>2)
                    {
                        Camera.main.fieldOfView -= 2;
                    }
                }
                if (!isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2))
                {
                    if (Camera.main.fieldOfView<100)
                    {
                        Camera.main.fieldOfView += 5;
                    }
                }
               
                oldPosition1 = tempPosition1;
                oldPosition2 = tempPosition2;
            }
        }
	}
    /// <summary>
    /// 函数返回真为放大,返回假为缩小
    /// </summary>
    bool isEnlarge(Vector2 oP1,Vector2 oP2,Vector2 nP1,Vector2 nP2)
    {
        //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
        var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1<leng2)
        {
            //放大的手势
            return true;
        }
        else
        {
            //缩小的手势
            return false;
        }
    }
    /// <summary>
    /// Update方法一旦调用结束后在这里算出重置摄像机的位置
    /// </summary>
    void LateUpdate()
    {
        //target为缩放旋转的参照物
        if (target)
        {
            //重置摄像机的位置
            y = ClampAngle(y, yMinLimit, yMaxLimit);
            Quaternion rotation = Quaternion.Euler(y, x, 0);
            Vector3 position = rotation * new Vector3(0, 0, -distance) + target.position;
            transform.rotation = rotation;
            transform.position = position;
        }
    }
    float ClampAngle(float angle,float min,float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle,min,max);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值