移动端双击位移,单指旋转,双指缩放


    private Camera camera = null;

    [HideInInspector]
    public float speed = 1f;

    private float angleHV, angleVV;
    [HideInInspector]
    public float smoothTime = 0.2f;
    float xSpeed;
    float ySpeed;
    private float lastDist = 0;
    private float curDist = 0;
    float angleHA = 45f;//Y轴旋转多少度
    float angleVA = 90f;//X轴旋转多少度
    private float curHAngle;
    private float curVAngle;
    private bool bMoving = false;
    private GameObject goSelected = null;
    bool roofOpen = false;

    void Start()
    {
        camera = this.GetComponentInChildren();
    }

    IEnumerator MoveToPosition(Vector3 endPos)
    {
        while (this.transform.localPosition != endPos)
        {
            this.transform.localPosition = Vector3.MoveTowards(this.transform.localPosition, endPos, 10 * Time.deltaTime * 5f);//移动速度
            // print(xx);
            yield return 0;
        }
    }

    void LateUpdate()
    {

        if (Input.touchCount == 2)   //代表有两个手指
        {
            ySpeed = 0f;//禁止转动
            xSpeed = 0f;
            if (Input.GetTouch(1).phase == TouchPhase.Began)
            {

            }
            //缩放
            if (Input.GetTouch(0).phase == TouchPhase.Moved &&    //第一个手指               
               Input.GetTouch(1).phase == TouchPhase.Moved)            //第二个手指
            {
                Vector3 s1 = Input.GetTouch(0).position;         //第一个手指屏幕坐标
                Vector3 s2 = Input.GetTouch(1).position;         //第二个手指屏幕坐标
                curDist = Vector2.Distance(s1, s2);
                float delta = Mathf.Abs(curDist - lastDist);
                if (delta > 0.1f && delta < 80f)
                {
                    camera.fov += Time.deltaTime * (lastDist - curDist) * 1.5f;
                    camera.fov = Mathf.Clamp(camera.fov, 5, 80);

                }
                lastDist = curDist;
            }
        }
        else
        {
            if (Input.touchCount == 1)
            {
                if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                {

                }
                else
                {

                    //拖动
                    if (Input.touches[0].phase == TouchPhase.Moved)
                    {
                        // 手指滑动时,要触发的代码 
                        //ySpeed = Mathf.Clamp( Input.GetAxis("Mouse X"),-5f,5f);    //手指水平移动的距离
                        //xSpeed = Mathf.Clamp( Input.GetAxis("Mouse Y"),-5f,5f);    //手指垂直移动的距离
                        angleHA += Input.GetTouch(0).deltaPosition.x * (360f / (float)Screen.width) * this.speed * 0.5f;
                        angleVA -= Input.GetTouch(0).deltaPosition.y * this.speed * (180f / (float)Screen.height) * 0.5f;
                        angleVA = Mathf.Clamp(angleVA, 10, 80f);//上下旋转角度
                        bMoving = true;
                    }

                    if (Input.touches[0].phase == TouchPhase.Began)
                    {
                        bMoving = false;
                    }

                    if (Input.touches[0].phase == TouchPhase.Ended)
                    {
                        if (bMoving) return;
                        RaycastHit hit;
                        Ray ray = camera.ScreenPointToRay(Input.touches[0].position);
                        bool isHit = Physics.Raycast(ray, out hit);
                        if (Input.touches[0].tapCount == 1)
                        {
                            if (isHit)
                            {
                                print(hit.collider.name);
                            }
                            if (hit.collider.name == "屋顶")//设备名称
                            {

                            }
                            else
                            {

                                //点中设备
                                if (hit.collider.tag == "SheBei")
                                {
                                    try
                                    {
                                        ///if (goSelected) goSelected.SetActive(false);
                                        ///goSelected = hit.transform.FindChild("Name").gameObject;
                                        ///goSelected.SetActive(true);

                                        /// HighlighterController hc = new HighlighterController();
                                        if (goSelected != null)
                                        {
                                            try
                                            {
                                                Destroy(goSelected.GetComponent());
                                                Destroy(goSelected.GetComponent());
                                            }
                                            catch { }
                                        }
                                        goSelected = hit.collider.gameObject;
                                        goSelected.AddComponent();


                                    }
                                    catch (Exception exp)
                                    {
                                    }
                                }
                                else//取消选择
                                {

                                    if (goSelected != null)
                                    {
                                        try
                                        {
                                            Destroy(goSelected.GetComponent());
                                            Destroy(goSelected.GetComponent());
                                        }
                                        catch { }
                                    }
                                    goSelected = null;
                                }
                            }//end 设备
                        }

                        else
                        {//双击移动
                            if (Input.touches[0].tapCount == 2)
                            {

                                if (isHit)
                                {

                                    if (hit.collider.tag == "Dimian")//设备标签
                                    {
                                        StopCoroutine("MoveToPosition");//调用协程
                                        StartCoroutine("MoveToPosition", hit.point);
                                    }
                                    if (hit.collider.tag == "SheBei")//设备标签
                                    {
                                        StopCoroutine("MoveToPosition");//调用协程
                                        StartCoroutine("MoveToPosition", hit.collider.transform.position);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        #region 旋转
        this.curHAngle = Mathf.SmoothDamp(this.curHAngle, angleHA, ref this.angleHV, this.smoothTime);
        this.curVAngle = Mathf.SmoothDamp(this.curVAngle, angleVA, ref this.angleVV, this.smoothTime);
        Vector3 eulerAngles = transform.eulerAngles;
        transform.eulerAngles = new Vector3(this.curVAngle, this.curHAngle, eulerAngles.z);
        #endregion

    }

 

你可能感兴趣的:(APP)