Unity 多平台下控制物体移动、旋转、缩放操作

using UnityEngine;
using System.Collections;
 
 //宏定义旋转观察
[AddComponentMenu("Camera-Control/3dsMax Camera Style")]
public class maxCamera : MonoBehaviour
{
    private Touch oldTouch1;  //上次触摸点1(手指1)  
    private Touch oldTouch2;  //上次触摸点2(手指2)  
    private Touch newTouch1;  //新触摸点1
    private Touch newTouch2;  //新触摸点

    Vector2 m_screenPos = new Vector2(); //记录手指触碰的位置

    public Transform target;
    public Vector3 targetOffset;
    public float distance = 5.0f;
    public float maxDistance =5;
    public float minDistance = 1.1f;
    public float xSpeed = 100.0f;
    public float ySpeed = 100.0f;
    public int yMinLimit = 0;
    public int yMaxLimit = 90;
    public int zoomRate = 40;
    public float panSpeed = 0.3f;
    public float zoomDampening = 5.0f;
 
    private float xDeg = 0.0f;
    private float yDeg = 0.0f;
    private float currentDistance;
    private float desiredDistance;
    private Quaternion currentRotation;
    private Quaternion desiredRotation;
    private Quaternion rotation;
    private Vector3 position;
 
    void Start() { Init(); }
    void OnEnable() { Init(); }
 
    public void Init()
    {
        //如果没有目标,创建一个临时的目标目前相机的“距离”的观点
        if (!target)
        {
            GameObject go = new GameObject("Cam Target");
            go.transform.position = transform.position + (transform.forward * distance);
            target = go.transform;
        }
 
        distance = Vector3.Distance(transform.position, target.position);
        currentDistance = distance;
        desiredDistance = distance;

        //当前旋转作为起点。
        position = transform.position;
        rotation = transform.rotation;
        currentRotation = transform.rotation;
        desiredRotation = transform.rotation;
 
        xDeg = Vector3.Angle(Vector3.right, transform.right );
        yDeg = Vector3.Angle(Vector3.up, transform.up );
    }

    void LateUpdate()
    {
#if UNITY_ANDROID  
        if (Input.touchCount <= 0)
        {
            return;

        }
       if (Input.touchCount==1)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
                m_screenPos = Input.touches[0].position;   //记录手指刚触碰的位置
            if (Input.touches[0].phase == TouchPhase.Moved) //手指在屏幕上移动,移动摄像机
            {
                //transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime, Input.touches[0].deltaPosition.y * Time.deltaTime, 0));
                xDeg += Input.touches[0].deltaPosition.x * xSpeed * 0.02f;
                yDeg -= Input.touches[0].deltaPosition.y * ySpeed * 0.02f;
            }
            //xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            //yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        }
       else if (Input.touchCount > 1)
       {
           //多点触摸, 放大缩小  
           newTouch1 = Input.GetTouch(0);
           newTouch2 = Input.GetTouch(1);
       }
           //第2点刚开始接触屏幕, 只记录,不做处理  
           if (newTouch2.phase == TouchPhase.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;

#endif


#if UNITY_EDITOR || UNITY_WEBGL || UNITY_STANDALONE_WIN    
           if (Input.GetMouseButton(1) || Input.GetMouseButton(2))
        {
            xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        }
#endif

        //夹角的垂直轴轨道
            yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
            // 设置相机旋转

            desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
            currentRotation = transform.rotation;
        
        
            rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
            transform.rotation = rotation;

#if UNITY_EDITOR ||UNITY_WEBGL|| UNITY_STANDALONE_WIN
            // affect the desired Zoom distance if we roll the scrollwheel
        desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
#endif

#if UNITY_ANDROID  
        desiredDistance -= offset * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
#endif
        //clamp the zoom min/max
        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        // For smoothing of the zoom, lerp distance
        currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
 
        // calculate position based on the new currentDistance 
        position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
        transform.position = position;
#if UNITY_ANDROID  
        //记住最新的触摸点,下次使用  
        oldTouch1 = newTouch1;
        oldTouch2 = newTouch2;
#endif
    }
 
    private 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);
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值