单位向量旋转N度后的新单位向量值

这边以绕y轴旋转为例,还有绕x、z轴的

public class Vector3RotationToADegree : MonoBehaviour
{
    public float m_degree;

    private void OnGUI()
    {
        Vector3 v = m_trans.forward;

        //C 语言里 double atan2(double y,double x) 返回的是原点至点(x,y)的方位角,即与 x 轴的夹角。
        //也可以理解为复数 x+yi 的辐角。返回值的单位为弧度
        //在Unity里z轴为x,x轴为y
        float radian = Mathf.Atan2(v.x, v.z);
        float x = Mathf.Sin(radian + m_degree * Mathf.Deg2Rad); //正弦值
        float z = Mathf.Cos(radian + m_degree * Mathf.Deg2Rad); //余弦值

        Vector3 newV = new Vector3(x, 0, z);
        GUI.Label(new Rect(10, 10, 400, 100), string.Format("{0} {1} {2}", v.x, v.z, radian));
        transform.rotation = Quaternion.LookRotation(newV);
    }
}



这个方法会影响其他轴,不是很好用!!!

//绕着某个轴旋转~
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class vector3CrossTest : MonoBehaviour
{
    public Transform m_obj1;
    public Transform m_obj2;
    public Transform m_obj3;
    public float angle;

    Vector3 start;
    Vector3 end;
    Vector3 end2;

    void Start ()
    {
    }
    

    void Update ()
    {
        start = m_obj1.position;
        end = m_obj2.position;
        end2 = m_obj3.position;

        Vector3 travelDirection = end - start;
        Vector3 levelDirecteion = end - new Vector3(start.x, end.y, start.z);

        Vector3 right = Vector3.Cross(travelDirection, levelDirecteion);
        
        Vector3 up = Vector3.Cross(right, travelDirection);
        if (end.y > start.y) up = -up;

//         Debug.DrawRay(start, travelDirection.normalized, Color.red);
         Debug.DrawLine(start, end, Color.red);
//         Debug.DrawRay(start, levelDirecteion.normalized, Color.gray);
//         Debug.DrawRay(start, right.normalized, Color.green);
//         Debug.DrawRay(start, up.normalized, Color.blue);

        Vector3 travelDirection2 = end2 - start;
        Debug.DrawLine(start, end2, Color.red);
        float len = Vector3.Distance(start, end2);

        for (int i=0; i<360; i++)
        {
            Vector3 k1 = Quaternion.AngleAxis(i, travelDirection.normalized) * travelDirection2.normalized;
            Vector3 k2 = Quaternion.AngleAxis(i+1, travelDirection.normalized) * travelDirection2.normalized;

            Debug.DrawLine(start + k1.normalized * len, start + k2.normalized * len, Color.cyan);
            Debug.DrawLine(start, start + k1.normalized * len, Color.blue);
        }
    }
}

你可能感兴趣的:(单位向量旋转N度后的新单位向量值)