当前位置:网站首页>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;
}
}
边栏推荐
- Learn good-looking custom scroll bars in 1 minute
- Getting started with webgl (4)
- Detailed explanation of Cocos creator 2.4.0 rendering process
- 【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
- Super signature principle (fully automated super signature) [Yun Xiaoduo]
- Summary of knowledge points of xlua hot update solution
- [Lanzhou University] information sharing of postgraduate entrance examination and re examination
- Function: JS Click to copy content function
- 无线传感器网络--ZigBee和6LoWPAN
- HW primary flow monitoring, what should we do
猜你喜欢
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
Three. JS introductory learning notes 10:three JS grid
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
AE learning 02: timeline
Three. JS introductory learning notes 15: threejs frame animation module
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
Keil5 does not support online simulation of STM32 F0 series
LeetCode2_ Add two numbers
[quick start of Digital IC Verification] 19. Basic grammar of SystemVerilog learning 6 (thread internal communication... Including practical exercises)
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
随机推荐
webgl_ Enter the three-dimensional world (1)
[quick start of Digital IC Verification] 23. AHB sramc of SystemVerilog project practice (3) (basic points of AHB protocol)
Virtual memory, physical memory /ram what
Migration and reprint
numpy--数据清洗
AE learning 01: AE complete project summary
Three. JS introductory learning notes 04: external model import - no material obj model
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
C4D learning notes 3- animation - animation rendering process case
The download button and debug button in keil are grayed out
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
nodejs package. JSON version number ^ and~
How to create Apple Developer personal account P8 certificate
TS typescript type declaration special declaration field number is handled when the key key
Using eating in cocos Creator
It's different for rich people to buy a house
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
Zhongang Mining: Fluorite continues to lead the growth of new energy market