10491人加入学习
(62人评价)
AR系列教程 - Vuforia入门 ( Unity 2017.3 )

制作完成于2018-01-16

价格 免费

利用两点间的距离公式(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)实现对于双指的距离判断

判断是扩大还是缩小

[展开全文]

可通过距离判断放大还是缩小

[展开全文]
public class Enlarge : MonoBehaviour {
    //左手手指
    Vector2 oldPos1;
    //右手手指
    Vector2 oldPos2;
    // Use this for initialization
    void Start() {
    } 
    // Update is called once per frame
    void Update() {
        if(Input.touchCount == 2){
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
//现在手指所在坐标
                Vector2 temPos1 = Input.GetTouch(0).position;
                Vector2 temPos2 = Input.GetTouch(1).position;

 

                if (isEnlarge(oldPos1, oldPos2, temPos1, temPos2))
                {
//放大
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale * 1.025f;
                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                else
                {
//缩小
                    float oldScale = transform.localScale.x;
                    float newScale = oldScale / 1.025f;
                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }
                oldPos1 = temPos1;
                oldPos2 = temPos2;
            }
        }
    }

 

 
 

 

    //判断手势

 

    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
    float length1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float length2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (length1 < length2) {
            return true;
        }
        else { return false; }
    }
}

 

 
 
[展开全文]

//放大缩小
Vector2 oldPos1;
Vector2 oldPos2;
void Update()
{
    if(Input.touchCount==2)
    {
        if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved)
        {
            Vector2 temPos1=Input.GetTouch(0).position;
            Vector2 temPos2=Input.GetTouch(1).position;
        
            if(isEnlarge(oldPos1,oldPos2,temPos1,temPos2))
            {
                float oldScale=transform.localScale.x;
                float newScalse=oldScale*1.025f;
                transform.localScale=new Vector3(newScalse,newScalse,newScalse);
            }
            if(isEnlarge(oldPos1,oldPos2,temPos1,temPos2))
            {
                float oldScale=transform.localScale.x;
                float newScalse=oldScale/1.025f;
                transform.localScale=new Vector3(newScalse,newScalse,newScalse);
            }
            oldPos1=temPos1;
            oldPos2=temPos2;
        }
    }
}

bool isEnlarge(Vector2 oP1,Vector2 oP2,Vector2 nP1,Vector2 nP2)
{
    float length1= Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y));
    float length2= Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y));
    if(length1<length2)
    {
    return true;

    }else
    {
    return false;
    }
}

[展开全文]

Mathf.Sqrt 开根号的方法 (勾股定理)

[展开全文]

授课教师

SIKI学院老师

课程特色

图文(1)
下载资料(1)
视频(24)

学员动态