当前位置:网站首页>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;
}
}
边栏推荐
- 用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
- HW primary flow monitoring, what should we do
- TS typescript type declaration special declaration field number is handled when the key key
- Getting started with webgl (1)
- Async and await
- [quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
- Clang compile link ffmpeg FAQ
- numpy---基础学习笔记
- 15. Using the text editing tool VIM
- LeetCode3_ Longest substring without duplicate characters
猜你喜欢
Getting started with webgl (1)
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
Three. JS introductory learning notes 11:three JS group composite object
Mesh merging under ue4/ue5 runtime
喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
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
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
LeetCode1_ Sum of two numbers
随机推荐
Yunxiaoduo software internal test distribution test platform description document
Gd32 F3 pin mapping problem SW interface cannot be burned
After UE4 is packaged, mesh has no material problem
[Lanzhou University] information sharing of postgraduate entrance examination and re examination
喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
numpy--数据清洗
尤雨溪,来了!
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Ida Pro reverse tool finds the IP and port of the socket server
Webcodecs parameter settings -avc1.42e01e meaning
Summary of knowledge points of xlua hot update solution
如何在shell中实现 backspace
A link opens the applet code. After compilation, it is easy to understand
Getting started with webgl (3)
Streaming end, server end, player end
Limit of total fields [1000] in index has been exceeded
Cocos creator collision and collision callback do not take effect
webgl_ Enter the three-dimensional world (2)
Getting started with webgl (2)