使用Mathf.Atan2() 计算的角度

本文介绍了弧度作为角度测量单位的概念及其与角度的换算方法。详细解释了如何使用Mathf.Atan2()函数来计算两点间的角度,并给出了具体实例。

目录

首先了解什么是弧度?

根据计算弧度计算角度


首先了解什么是弧度?

弧度 ( rad ) : 弧度是角的度量单位。

定义:弧长等于半径的弧,其所对的圆心角为1弧度。

根据定义,一周的弧度数为 2πr/r = 2π ,360°角=2π弧度,因此,1弧度约为57.3°,即57°17'44.806'',1°为π/180弧度,近似值为0.01745弧度,周角为2π弧度,平角(即180°角)为π弧度,直角为π/2弧度。

根据计算弧度计算角度

举个简单的例子。有两个点,暂且 A 点为原点。 

A、B 两个点。

    Vector2 A = new Vector2(x, y);
    Vector2 B = new Vector2(x, y);

1.使用 Mathf.Atan2()  计算点与 X轴 的夹角的弧度。

var radian = Mathf.Atan2(B.y - A.y, B.x - A.x);

 

 2.使用 Mathf.Atan2()  计算点与 Y轴 的夹角的弧度。

var radian = Mathf.Atan2(B.x - A.x, B.y - A.y);

 

 根据弧度计算角度

var angle = (radian * (180 / Math.PI);

180 / Math.PI 一弧度对应的角度

*需要注意的点:

  • Mathf.Atan2()的返回逆时针角度,以弧度为单位,结果为 点 (x, y)与原点连线 和 正X轴 之间的夹角
  • Mathf.Atan2()的返回值范围 [ -π , π ]
  • 弧度是一个逆时针方向计算出来的,而我们旋转的时候是按正时针方向旋转,所以我们用的时候要先进行取反: angle = -angle 

实例

 

//在一定的角度时候拖拽物体,否则不拖拽

Vector2 vector2 = Input.mousePosition;
var radian = Mathf.Atan2(vector2.y - _startPos.y, vector2.x - _startPos.x);
var angle = Mathf.Abs((float) (radian * (180 / Math.PI)));
if (angle > 45 && angle < 135)
    Debug.Log("拖拽");
else
    Debug.Log("不拖拽");

参考

https://www.hangge.com/blog/cache/detail_1087.html

通过Math.atan2计算角度 改变物体朝向 - 蚁丘 - 博客园

using UnityEngine; using Lean.Touch; using UnityEngine.EventSystems; public class touchCamCon : MonoBehaviour { public Camera targetCam; public Transform target; // 目标物体 public float rotationSpeed = 1f; // 旋转灵敏度 public float baseDistance = 5f; // 初始距离(备用) public float minDistance = 2f; // 最小距离 public float maxDistance = 10f; // 最大距离 public float zoomSpeed = 1f; // 缩放速度(单位距离/秒) public float minVerticalAngle = -30f; // 垂直旋转最小角度 public float maxVerticalAngle = 60f; // 垂直旋转最大角度 private float currentDistance; // 当前实际距离 private Vector3 offset; // 相机与目标的偏移量 private float currentVerticalAngle = 0f; // 当前垂直旋转角度 private float targetDistance; // 目标距离(用于平滑过渡) public LeanFingerFilter Use = new LeanFingerFilter(true); void Start() { if (target == null) { Debug.LogError("目标物体未设置!"); return; } // 1. 计算初始偏移量和距离(基于编辑器中的相机位置) offset = transform.position - target.position; currentDistance = offset.magnitude; // 2. 精确计算初始垂直角度(考虑目标物体的旋转) Vector3 localOffset = target.InverseTransformDirection(offset); currentVerticalAngle = Mathf.Atan2(localOffset.y, Mathf.Sqrt(localOffset.x * localOffset.x + localOffset.z * localOffset.z)) * Mathf.Rad2Deg; // 3. 强制限制初始距离和角度 currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance); currentVerticalAngle = Mathf.Clamp(currentVerticalAngle, minVerticalAngle, maxVerticalAngle); targetDistance = currentDistance; // 4. 立即更新相机位置和朝向 UpdateCameraPosition(); } void Update() { var fingers = Use.UpdateAndGetFingers(); if (target == null) return; // 1. 单指拖拽:水平/垂直旋转 if (fingers.Count == 1) { Vector2 fingerDelta = LeanGesture.GetScreenDelta(fingers); if (fingerDelta.magnitude < 0.1f) return; // 水平旋转(绕Y轴) float horizontalDelta = fingerDelta.x; if (Mathf.Abs(horizontalDelta) > 0.1f) { transform.RotateAround( target.position, Vector3.up, horizontalDelta * rotationSpeed * 0.1f ); } // 垂直旋转(绕相机右轴,带限制) float verticalDelta = fingerDelta.y; if (Mathf.Abs(verticalDelta) > 0.1f) { float newVerticalAngle = currentVerticalAngle - verticalDelta * rotationSpeed * 0.1f; newVerticalAngle = Mathf.Clamp(newVerticalAngle, minVerticalAngle, maxVerticalAngle); transform.RotateAround( target.position, transform.right, newVerticalAngle - currentVerticalAngle ); currentVerticalAngle = newVerticalAngle; } offset = transform.position - target.position; transform.LookAt(target); } // 2. 双指缩放:基于距离增量的缩放 else if (fingers.Count >= 2) { float pinchScale = LeanGesture.GetPinchScale(fingers); if (Mathf.Abs(pinchScale - 1f) > 0.01f) { float distanceDelta = (pinchScale - 1f) * zoomSpeed; targetDistance = Mathf.Clamp(currentDistance - distanceDelta, minDistance, maxDistance); } } // 平滑过渡到目标距离 currentDistance = Mathf.Lerp(currentDistance, targetDistance, Time.deltaTime * 5f); UpdateCameraPosition(); } // 根据当前参数更新相机位置和朝向 private void UpdateCameraPosition() { // 1. 计算方向(考虑垂直角度和目标物体的旋转) Vector3 direction = Quaternion.Euler(currentVerticalAngle, transform.eulerAngles.y, 0f) * Vector3.back; Vector3 worldDirection = target.TransformDirection(direction); // 2. 更新位置和朝向 transform.position = target.position + worldDirection.normalized * currentDistance; transform.LookAt(target); } } 这个代码怎么改成 如果手指在ui上就不起作用
07-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值