纳金网

标题: 鼠标拖动UGUI 进行旋转的代码 [打印本页]

作者: may    时间: 2018-7-22 01:39
标题: 鼠标拖动UGUI 进行旋转的代码
  1. private Quaternion q;
  2. private Vector3 mousePos;
  3. private Vector3 preMousePos;
  4. private Vector3 modelPos;
  5. private Vector3 localEluer;
  6. private bool IsSelect = false;
  7. private float RotateAngle;
  8. private float angle;
  9. public Transform target1;

  10. void Start()
  11. {
  12.     modelPos = target1.transform.position;
  13.     angle = localEluer.z;
  14.     target1.transform.localEulerAngles = localEluer;
  15. }
  16. void Update()
  17. {

  18.     if (Input.GetMouseButtonDown(0))
  19.     {
  20.         IsSelect = true;
  21.         preMousePos = mousePos = Input.mousePosition;
  22.     }
  23.     if (Input.GetMouseButton(0) && IsSelect)
  24.     {
  25.         IsSelect = true;
  26.         mousePos = Input.mousePosition;
  27.         RotateAngle = Vector3.Angle(preMousePos - modelPos, mousePos - modelPos);
  28.         //print (RotateAngle);

  29.         if (RotateAngle == 0)
  30.         {
  31.             preMousePos = mousePos;
  32.         }
  33.         else
  34.         {
  35.             q = Quaternion.FromToRotation(preMousePos - modelPos, mousePos - modelPos);
  36.             float k = q.z > 0 ? 1 : -1;
  37.             localEluer.z += k * RotateAngle;

  38.             Debug.Log(localEluer.x);

  39.             angle = localEluer.z = Mathf.Clamp(localEluer.z, -36000, 36000);

  40.             target1.transform.localEulerAngles = localEluer;
  41.             preMousePos = mousePos;
  42.         }

  43.     }
  44.     if (Input.GetMouseButtonUp(0))
  45.     {
  46.         IsSelect = false;
  47.     }
  48. }
复制代码
来自:1320494699 的分享





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5