二维物体跟随鼠标移动

理解

mousePos - transform.position 鼠标的位置减去挂在此脚本上物体的位置

 

z = -Vector3.Angle(Vector3.up, mousePos - transform.position); Vector3.up为(0,1,0),
这句话暂时理解为(0,1,0)mousePos - transform.position夹角

 

  //Canvans下的物体
 2     public RectTransform UGUICanvans;
 3     //当前主相机
 4     public Camera mainCamera;
 5 
 6     void Update()
 7     {
 8         Vector3 mousePos;
 9         //ScreenPointToWorldPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector3 worldPoint);
10         RectTransformUtility.ScreenPointToWorldPointInRectangle(UGUICanvans, new Vector2(Input.mousePosition.x, Input.mousePosition.y),mainCamera, out mousePos);
11         float z;
12         if (mousePos.x > transform.position.x)
13         {
14             //返回的鼠标的位置-transform.position枪的位置
15             z = -Vector3.Angle(Vector3.up, mousePos - transform.position);
16             
17         }
18         else
19         {
20             z = Vector3.Angle(Vector3.up, mousePos - transform.position);
21         }
22         transform.localRotation = Quaternion.Euler(0, 0, z);
23     }

 

 

二维物体跟随鼠标移动

上一篇:Android异步框架RxJava 1.x系列(三) - 线程调度器Scheduler


下一篇:appium+python的APP自动化(2)