当前位置:网站首页>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;
}
}
边栏推荐
- Shader Language
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- LeetCode2_ Add two numbers
- 保证接口数据安全的10种方案
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- Super signature principle (fully automated super signature) [Yun Xiaoduo]
- Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
- webgl_ Enter the three-dimensional world (1)
- Migration and reprint
- 10 schemes to ensure interface data security
猜你喜欢
Syntax of generator function (state machine)
Actually changed from 408 to self proposition! 211 North China Electric Power University (Beijing)
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
Yunxiaoduo software internal test distribution test platform description document
Whole process analysis of unity3d rendering pipeline
Three. JS introductory learning notes 19: how to import FBX static model
10 schemes to ensure interface data security
The difference between full-time graduate students and part-time graduate students!
【数字IC验证快速入门】19、SystemVerilog学习之基本语法6(线程内部通信...内含实践练习)
Tkinter after how to refresh data and cancel refreshing
随机推荐
TS as a general cache method
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
Detailed explanation of Cocos creator 2.4.0 rendering process
numpy--疫情数据分析案例
Use moviepy Editor clips videos and intercepts video clips in batches
Please supervise the 2022 plan
Android -- jetpack: the difference between livedata setValue and postvalue
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
webgl_ Enter the three-dimensional world (2)
A wave of open source notebooks is coming
Three. JS introductory learning notes 10:three JS grid
Async and await
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Syntax of generator function (state machine)
【数字IC验证快速入门】20、SystemVerilog学习之基本语法7(覆盖率驱动...内含实践练习)
[quick start of Digital IC Verification] 29. Ahb-sramc (9) (ahb-sramc svtb overview) of SystemVerilog project practice
Three. JS introductory learning notes 11:three JS group composite object
Zhongang Mining: Fluorite continues to lead the growth of new energy market
AB package details in unity (super detail, features, packaging, loading, manager)
持续创作,还得靠它!