当前位置:网站首页>The unity vector rotates at a point
The unity vector rotates at a point
2022-07-07 15:53:00 【Le_ Sam】
This article is an example for you to share Unity The specific code that the vector rotates according to a certain point , For your reference , The details are as follows
One 、unity The rotation of the
The first thing to know is that Unity Quaternions have been used in the rotation of , If for an object rotation You will find that the result is not what you want in the end , At this time, we need to use Quaternion To rotate .
Two 、 The vector rotates according to the origin
Use of Unity Built-in methods Quaternion.AngleAxis(float angle,Vector3 axis)
The first parameter is the angle we need to rotate angle Greater than 0 It rotates clockwise ,angle Less than 0 It rotates counterclockwise , The rotation here is based on the coordinate origin .
The second parameter is the axis of rotation , About which axis .
Be careful : When using this method, we also get quaternions , We convert it into a vector Vector3 It needs to be multiplied by its own coordinates ( Four yuan number * Self vector , If I go the other way Self vector * Four yuan number stay Unity A compilation error occurs , There's a little bit of caution here )
Case study : take Vector3(1,0,1) Rotate according to the origin 45°,90°,180°,270° Test with black 、 yellow 、 blue 、 Green means
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);
}
}
3、 ... and 、 The vector rotates according to the specified position
/// <summary>
/// Rotate a specified angle around a point
/// </summary>
/// <param name="position"> Self coordinate </param>
/// <param name="center"> Center of rotation </param>
/// <param name="axis"> Around the axis of rotation </param>
/// <param name="angle"> Rotation Angle </param>
/// <returns></returns>
public Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
{
return Quaternion.AngleAxis(angle, axis) * (position - center) + center;
}
Case study : take Vector3(1,0,1) according to Vector(2,0,2) rotate 45°,90°,180°,270° Test with red 、 yellow 、 blue 、 Green means
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>
/// Rotate a specified angle around a point
/// </summary>
/// <param name="position"> Self coordinate </param>
/// <param name="center"> Center of rotation </param>
/// <param name="axis"> Around the axis of rotation </param>
/// <param name="angle"> Rotation Angle </param>
/// <returns></returns>
public Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle)
{
return Quaternion.AngleAxis(angle, axis) * (position - center) + center;
}
}
边栏推荐
- Three. JS introductory learning notes 13: animation learning
- 20th anniversary of agile: a failed uprising
- C Alibaba cloud OSS file upload, download and other operations (unity is available)
- HW primary flow monitoring, what should we do
- The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
- Monthly observation of internet medical field in May 2022
- OpenGL common functions
- Vite path alias @ configuration
- Ue4/ue5 multi thread development attachment plug-in download address
- Getting started with webgl (1)
猜你喜欢
numpy---基础学习笔记
【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
Yunxiaoduo software internal test distribution test platform description document
[quick start of Digital IC Verification] 25. AHB sramc of SystemVerilog project practice (5) (AHB key review, key points refining)
Streaming end, server end, player end
无线传感器网络--ZigBee和6LoWPAN
JS array foreach source code parsing
Postman generate timestamp, future timestamp
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
【微信小程序】Chapter(5):微信小程序基础API接口
随机推荐
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
A wave of open source notebooks is coming
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
Summary of knowledge points of xlua hot update solution
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
保证接口数据安全的10种方案
【微信小程序】Chapter(5):微信小程序基础API接口
Whether runnable can be interrupted
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
Ida Pro reverse tool finds the IP and port of the socket server
Monthly observation of internet medical field in May 2022
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
C4D learning notes 3- animation - animation rendering process case
numpy--疫情数据分析案例
LeetCode1_ Sum of two numbers
Detailed explanation of Cocos creator 2.4.0 rendering process
如何在shell中实现 backspace