鼠标顺时针旋转、逆时针旋转

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class ShouShi : MonoBehaviour
 5 {
 6     public Transform centerTra;//围绕旋转的中心
 7     public Transform targetTra;//移动目标
 8     private Vector2 oldPos;//上一帧位置
 9     private float angular;
10     private float speed = 0.1f;//升降速度
11     void Update()
12     {
13         if (Input.GetMouseButtonDown(0))
14         {
15             oldPos = Input.mousePosition;
16         }
17         
18         if (Input.GetMouseButton(0))
19         {
20             Vector3 point = Camera.main.WorldToScreenPoint(centerTra.position );//中心点转换为屏幕坐标
21             angular = SignedAngularGap(new Vector2(point.x, point.y), oldPos, Input.mousePosition);
22             print(angular);
23             float y = targetTra.position.y;
24             y += angular * speed * Time.deltaTime;
25             targetTra.position = new Vector3(targetTra.position.x, y, targetTra.position.z);
26             oldPos = Input.mousePosition;
27         }
28     }
29 
30     public static float SignedAngle(Vector2 from, Vector2 to)
31     {
32         // perpendicular dot product
33         float perpDot = (from.x * to.y) - (from.y * to.x);
34         return Mathf.Atan2(perpDot, Vector2.Dot(from, to));
35     }
36     // return signed angle in degrees between current finger position and ref positions
37     static float SignedAngularGap(Vector2 center, Vector2 oldPos, Vector2 newPos)
38     {
39         Vector2 refDir = (oldPos - center).normalized;
40         Vector2 curDir = (newPos - center).normalized;
41 
42         // check if we went past the minimum rotation amount treshold
43         return Mathf.Rad2Deg * SignedAngle(refDir, curDir);
44     }
45 }

 

转载于:https://www.cnblogs.com/yhx8224/p/5243080.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值