模型的操作

来源:互联网 发布:linux mmap 编辑:程序博客网 时间:2024/06/01 10:11
using UnityEngine;
using System.Collections;

public class ModelRotate : MonoBehaviour
{
    public float speed = 100.0f;

    //缩放系数     
    float distance = 10.0f;
 
    float yMinLimit = -20;     
    float yMaxLimit = 80;     

    //记录上一次手机触摸位置判断用户是在左放大还是缩小手势
    private Vector2 oldPosition1;     
    private Vector2 oldPosition2;

    Transform myTrans;
    // Use this for initialization
    void Start()
    {
        myTrans = this.transform;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount == 1)
        //if (Input.GetMouseButton(0))
        {
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {

                float h = Input.GetAxis("Mouse X");
                float v = Input.GetAxis("Mouse Y");

                if (Mathf.Abs(h) >= Mathf.Abs(v))
                {
                    if (h < 0)
                    {
                        myTrans.Rotate(Vector3.upTime.deltaTime * speed);
                    }
                    if (h > 0)
                    {
                        myTrans.Rotate(-Vector3.upTime.deltaTime * speed);
                    }

                }
            }
        }
        //判断触摸数量为多点触摸     
        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(oldPosition1oldPosition2tempPosition1tempPosition2))
                {
                    //放大系数超过3以后不允许继续放大     
                    //这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改     
                    //if (distance > 3)
                    {
                        distance -= 0.5f;
                        myTrans.localScale *= 1.05f;
                    }
                }
                else
                {
                    //缩小洗漱返回18.5后不允许继续缩小     
                    //这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改     
                    //if (distance < 18.5)
                    {
                        distance += 0.5f;
                        myTrans.localScale *= 0.95f;
                    }
                }
                //备份上一次触摸点的位置,用于对比     
                oldPosition1 = tempPosition1;
                oldPosition2 = tempPosition2;
            }
        }
    }
    
               
    //函数返回真为放大,返回假为缩小     
    bool isEnlarge(Vector2 oP1Vector2 oP2Vector2 nP1Vector2 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;      
        }     
    }
}
0 0
原创粉丝点击