Unity vector rotation performed according to a point

A, unity of rotation

First thing to know is used in rotation quaternion Unity in the rotation, if direct assignment of rotation of an object you'll find the final result is not the result you want, and this time we need to go to the aid of Quaternion to rotate.

Second, rotating in the origin vector

Unity built-in method used Quaternion.AngleAxis (float angle, Vector3 axis)
The first argument is that we need the angle of rotation angle greater than 0 is rotated in the clockwise direction, the rotational angle is less than 0 according to the anticlockwise direction, the coordinate origin of the rotation of the rotation here.
The second parameter is a rotation shaft, which rotates around one axis.

Note: quaternion is obtained using this method, we convert it into a vector Vector3 is multiplied by its coordinates (quaternion * vector itself, if in turn own vector * quaternion Unity occurs at compile error here it should be noted)
Case: The Vector3 (1,0,1) according to the origin of rotation 45 °, 90 °, 180 °, 270 ° respectively tested black, yellow, blue, green color represents

Here Insert Picture Description
code show as below:

using UnityEngine;

[ExecuteInEditMode]
public class VectorDirTest : MonoBehaviour {

	// Update is called once per frame
	void Update () {
        Debug.DrawLine(Vector3.left * 5f,Vector3.right * 5f,Color.red);
        Debug.DrawLine(Vector3.up * 5f,Vector3.down * 5f,Color.green);
        Debug.DrawLine(Vector3.forward * 5f,Vector3.back * 5f,Color.blue);

       
        Vector3 dir = new Vector3(1,0,1);
        Debug.DrawLine(Vector3.zero,dir,Color.white);

        Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(45,Vector3.up) * dir,Color.black);

        Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(90,Vector3.up) * dir,Color.yellow);

        Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(180,Vector3.up) * dir,Color.blue);

        Debug.DrawLine(Vector3.zero,Quaternion.AngleAxis(270,Vector3.up) * dir,Color.green);

    }
}

Third, according to the specified rotation position vector


    /// <summary>
    /// 围绕某点旋转指定角度
    /// </summary>
    /// <param name="position">自身坐标</param>
    /// <param name="center">旋转中心</param>
    /// <param name="axis">围绕旋转轴</param>
    /// <param name="angle">旋转角度</param>
    /// <returns></returns>
    public Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
    {
        return Quaternion.AngleAxis(angle, axis) * (position - center) + center;
    }
Case: The Vector3 (1,0,1) (2,0,2) is rotated in the Vector 45 °, 90 °, 180 °, 270 ° were tested with red, yellow, blue, green color represents

Here Insert Picture Description

code show as below:

using UnityEngine;

[ExecuteInEditMode]
public class VectorDirTest : MonoBehaviour {



	
	// Update is called once per frame
	void Update () {
        Debug.DrawLine(Vector3.left * 5f,Vector3.right * 5f,Color.red);
        Debug.DrawLine(Vector3.up * 5f,Vector3.down * 5f,Color.green);
        Debug.DrawLine(Vector3.forward * 5f,Vector3.back * 5f,Color.blue);

        Vector3 dir = new Vector3(1,0,1);
        Vector3 point = new Vector3(2,0,2);

        Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 45), Color.red);
        Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 90), Color.yellow);
        Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 180), Color.blue);
        Debug.DrawLine(Vector3.zero, RotateRound(dir, point, Vector3.up, 270), Color.green);
        Debug.DrawLine(Vector3.zero, dir, Color.black);

        
    }

    

    /// <summary>
    /// 围绕某点旋转指定角度
    /// </summary>
    /// <param name="position">自身坐标</param>
    /// <param name="center">旋转中心</param>
    /// <param name="axis">围绕旋转轴</param>
    /// <param name="angle">旋转角度</param>
    /// <returns></returns>
    public Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
    {
        return Quaternion.AngleAxis(angle, axis) * (position - center) + center;
    }
}

Published 62 original articles · won praise 5 · Views 3898

Guess you like

Origin blog.csdn.net/qq_42194657/article/details/104019679