unity 物体跟随鼠标指针 移动

Cusor.lockState=CusorLockMode.Locked;   //锁定光标不能动    注:可按Esc解锁光标

Cusor.visible=false   //隐藏光标   

private Vector3 screenPosition;
        private Vector3 mousePositionOnScreen;
        private Vector3 mousePositionInWorld;
        void MouseFollow()
        {
            //物体的世界坐标转换为屏幕坐标
            screenPosition = Camera.main.WorldToScreenPoint(dragObj.transform.position);
            //获取鼠坐标
            mousePositionOnScreen = Input.mousePosition;
            //让鼠标坐标的Z=物体的坐标Z
            mousePositionOnScreen.z = screenPosition.z;
            //将鼠标坐标转化为世界坐标
            mousePositionInWorld = Camera.main.ScreenToWorldPoint(mousePositionOnScreen);
  //物体用于呈现在相机 面前(相机是UI相机 可用。如果是场景里的3d相机,请注释这句,并将物体放在3d下,为其子物体  自动跟随)
            mousePositionInWorld.z = Camera_.transform.position.z + 5;
            

//物体跟随鼠标移动
            dragObj.transform.position = mousePositionInWorld;
        }

/// 
        /// 移动和旋转物品流程   
        /// 
        /// 
        /// 
        /// 
        /// 
        public void MoveAndRotate(Transform transform, Vector3 pos, Vector3 rot, float moveTime)
        {
            //TypeEventSystem.Send();

            transform.DOLocalRotate(rot, moveTime);
            transform.DOLocalMove(pos, moveTime).OnComplete(() => { });
        }

你可能感兴趣的:(Unity,unity,计算机外设,游戏引擎)