from | 测量角度差的起始向量。 |
to | 测量角度差的目标向量。 |
float 两个向量之间的有符号角度(以度为单位)。
获取 from
和 to
之间的有符号角度(以度为单位)。
返回的角度是两个向量之间的有符号逆时针角度。
注意:返回的角度将始终介于 -180 度到 180 度之间,因为该方法返回向量之间的最小角度。也就是说,它永远不会返回优角。角度是从世界原点 (0,0,0) 作为顶点计算的。
其他资源:Angle 函数。
using UnityEngine;
public class Vector : MonoBehaviour { //Use these to get the GameObject's positions Vector2 m_MyFirstVector; Vector2 m_MySecondVector;
float m_Angle;
//You must assign to these two GameObjects in the Inspector public GameObject m_MyObject; public GameObject m_MyOtherObject;
void Start() { //Initialise the Vector m_MyFirstVector = Vector2.zero; m_MySecondVector = Vector2.zero; m_Angle = 0.0f; }
void Update() { //Fetch the first GameObject's position m_MyFirstVector = new Vector2(m_MyObject.transform.position.x, m_MyObject.transform.position.y); //Fetch the second GameObject's position m_MySecondVector = new Vector2(m_MyOtherObject.transform.position.x, m_MyOtherObject.transform.position.y); //Find the angle for the two Vectors m_Angle = Vector2.SignedAngle(m_MyFirstVector, m_MySecondVector);
//Draw lines from origin point to Vectors Debug.DrawLine(Vector2.zero, m_MyFirstVector, Color.magenta); Debug.DrawLine(Vector2.zero, m_MySecondVector, Color.blue);
//Log values of Vectors and angle in Console Debug.Log("MyFirstVector: " + m_MyFirstVector); Debug.Log("MySecondVector: " + m_MySecondVector); Debug.Log("Angle Between Objects: " + m_Angle); }
void OnGUI() { //Output the angle found above GUI.Label(new Rect(25, 25, 200, 40), "Angle Between Objects" + m_Angle); } }