三维物体跟随鼠标移动

 //上下 x(-45,15) 
    //左右 y(-60,60) 
    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotation = 60;
    private float minXRotation = 0;
 void Update()
    {
       //计算出鼠标在屏幕上的位置
            float xPosPrecent = Input.mousePosition.x / Screen.width;
            float yPosPrecent = Input.mousePosition.y / Screen.height;

            //Mathf.Clamp 限制value的值在min和max之间 如果value小于min,返回min。如果value大于max,返回max,否则返回value 
            float xAngele = -Mathf.Clamp(yPosPrecent * maxXRotation, minXRotation, maxXRotation) + 15;
            float yAngele = Mathf.Clamp(xPosPrecent * maxYRotation, minYRotation, maxYRotation) - 60;

            transform.eulerAngles = new Vector3(xAngele, yAngele, 0);
        }
      
    }

 

posted on 2018-09-26 18:06  追求LPY  阅读(437)  评论(0编辑  收藏  举报