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);
            }
            

        }

 

    }
}
 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用Unity的Input类来实现手势控制。下面是一个简单的示例代码: ```csharp using UnityEngine; public class ObjectController : MonoBehaviour { private float rotateSpeed = 0.5f; private float moveSpeed = 0.1f; private float scaleSpeed = 0.1f; private Vector2 prevTouchPos; private Vector2 touchDelta; void Update() { if (Input.touchCount == 1) { // 单指滑动控制物体旋转 if (Input.GetTouch(0).phase == TouchPhase.Moved) { float rotateX = Input.GetTouch(0).deltaPosition.x * rotateSpeed; float rotateY = Input.GetTouch(0).deltaPosition.y * rotateSpeed; transform.Rotate(Vector3.up, -rotateX, Space.World); transform.Rotate(Vector3.right, rotateY, Space.World); } } else if (Input.touchCount == 2) { // 双指滑动控制物体左右上下移动 if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved) { touchDelta = Input.GetTouch(0).position - Input.GetTouch(1).position; float moveX = touchDelta.x * moveSpeed; float moveY = touchDelta.y * moveSpeed; transform.Translate(-moveX, -moveY, 0, Space.World); } // 双指分开缩小控制物体放大缩小 float pinchAmount = 0; Quaternion desiredRotation = transform.rotation; if (Input.GetTouch(1).phase == TouchPhase.Began) { prevTouchPos = Input.GetTouch(1).position - Input.GetTouch(0).position; } else if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved) { var curTouchPos = Input.GetTouch(1).position - Input.GetTouch(0).position; pinchAmount = curTouchPos.magnitude - prevTouchPos.magnitude; desiredRotation *= Quaternion.Euler(Vector3.back * pinchAmount * scaleSpeed); prevTouchPos = curTouchPos; } transform.rotation = desiredRotation; transform.localScale += new Vector3(pinchAmount, pinchAmount, pinchAmount); } } } ``` 在这个示例中,我们使用了两个变量 `rotateSpeed` 和 `moveSpeed` 来控制旋转和移动的速度,使用变量 `scaleSpeed` 来控制缩放的速度。`prevTouchPos` 变量用于存储双指滑动的起始位置,`touchDelta` 变量用于存储双指滑动距离差。在代码中,我们使用 `transform` 属性来控制物体的旋转、移动和缩放

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值