1. 程式人生 > >Unity 模型在移動端進行移動、旋轉和放大(縮小)

Unity 模型在移動端進行移動、旋轉和放大(縮小)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateControl : MonoBehaviour
{

    //float xSpeed = 100f;
    //float ySpeed = 100f;
    float x = 0f;
    float y = 0f;
    float Speed = 0.01f;


    /// <summary>  
    /// 第一次按下的位置  
    /// </summary>  
    private Vector3 first = Vector3.zero;
    /// <summary>  
    /// 滑鼠的拖拽位置(第二次的位置)  
    /// </summary>  
    private Vector3 second = Vector3.zero;
    /// <summary>  
    /// 旋轉的角度  
    /// </summary>  
    //private float angle = 3f;
    /// <summary>  
    /// 記錄當前的方向
    /// </summary>
    public GameObject model;

    Vector2 oldPosition1;
    Vector2 oldPosition2;

    void Update()
    {
        if (Input.anyKey)
        {
            //當只有一次觸控
            if (Input.touchCount == 1)
            {
                //觸控型別,滑動
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    //移動拖拽
                    //獲取x軸
                    x = Input.GetAxis("Mouse X") * Speed;
                    //獲取y軸
                    y = Input.GetAxis("Mouse Y") * Speed;
                    model.transform.Translate(-x, y, 0);//*Time.deltaTime


                    //旋轉
                    ////獲取x軸
                    //x = Input.GetAxis("Mouse X") * xSpeed;
                    ////獲取y軸
                    //y = Input.GetAxis("Mouse Y") * ySpeed;
                    ////對模型進行上下左右旋轉
                    //model.transform.Rotate(Vector3.up * x * Time.deltaTime, Space.World);
                    //model.transform.Rotate(Vector3.left * -y * Time.deltaTime, Space.World);
                }

            }
            //當兩次觸控
            if (Input.touchCount > 1)
            {
                //兩次觸控都有滑動
                if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
                {
                    //獲取第一、二次兩次觸控的位置
                    Vector2 tempPosition1 = Input.GetTouch(0).position;
                    Vector2 tempPosition2 = Input.GetTouch(1).position;
                    //放大
                    if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                    {
                        float oldScale = model.transform.localScale.x;
                        float newScale = oldScale * 1.025f;
                        model.transform.localScale = new Vector3(newScale, newScale, newScale);
                    }
                    else//縮小
                    {
                        float oldScale = model.transform.localScale.x;
                        float newScale = oldScale / 1.025f;
                        model.transform.localScale = new Vector3(newScale, newScale, newScale);

                    }
                    //備份上一次觸控點的位置,用於對比   
                    oldPosition1 = tempPosition1;
                    oldPosition2 = tempPosition2;
                }
            }
        }
        
    }
    /// <summary>
    /// 比較兩次的位置,大小,來進行放大還是縮小
    /// </summary>
    /// <param name="oP1"></param>
    /// <param name="oP2"></param>
    /// <param name="nP1"></param>
    /// <param name="nP2"></param>
    /// <returns></returns>
    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;
        }
    }
}