In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how the Unity vector rotates according to a certain point. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. Rotation of unity
First of all, it is important to know that quaternions have been used in the rotation of Unity. If you directly assign a value to the rotation of an object, you will find that the result is not what you want in the end. At this time, we need to rotate with the help of Quaternion.
Second, the vector rotates according to the origin
The first parameter of the Unity built-in method Quaternion.AngleAxis (float angle,Vector3 axis) is that when the angle angle is greater than 0, it rotates clockwise, and when angle is less than 0, it rotates counterclockwise, where the rotation is carried out according to the coordinate origin. The second parameter is the axis of rotation, about which axis to rotate.
Note: quaternion is also obtained when using this method. When we convert it to vector Vector3, we need to multiply it by its own coordinates (quaternion * self vector, if the reverse self vector * quaternion will cause compilation errors in Unity, please note here)
Case study: the Vector3 is rotated 45 °, 90 °, 180 °and 270 °according to the origin with black, yellow, blue and green colors respectively.
The code is as follows:
Using UnityEngine; [ExecuteInEditMode] public class VectorDirTest: MonoBehaviour {/ / Update is called once per frame void Update () {Debug.DrawLine (Vector3.left * 5f); Debug.DrawLine (Vector3.up * 5f); Debug.DrawLine (Vector3.forward * 5f); Vector3 dir = new Vector3 (new Vector3); Debug.DrawLine (Vector3.zero,dir,Color.white) Debug.DrawLine (Vector3.zero,Quaternion.AngleAxis (45 recordVector3.up) * dir,Color.black); Debug.DrawLine (Vector3.zero,Quaternion.AngleAxis (90recordVector3.up) * dir,Color.yellow); Debug.DrawLine (Vector3.zero,Quaternion.AngleAxis (180coVector3.up) * dir,Color.blue); Debug.DrawLine (Vector3.zero,Quaternion.AngleAxis (270penceVector3.up) * dir,Color.green);}}
Third, the vector rotates according to the specified position
/ rotate the specified angle around a point / self-coordinate / rotation center / around the rotation axis / rotation angle / public Vector3 RotateRound (Vector3 position, Vector3 center, Vector3 axis, float angle) {return Quaternion.AngleAxis (angle, axis) * (position-center) + center;}
Case study: according to Vector, Vector3 was rotated 45 °, 90 °, 180 °and 270 °respectively in red, yellow, blue and green colors.
The code is as follows:
Using UnityEngine; [ExecuteInEditMode] public class VectorDirTest: MonoBehaviour {/ / Update is called once per frame void Update () {Debug.DrawLine (Vector3.left * 5f); Debug.DrawLine (Vector3.up * 5f); Debug.DrawLine (Vector3.forward * 5f); Vector3 dir = new Vector3 (1m); Vector3 point = new Vector3 (2m) 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, Color.green); Debug.DrawLine (Vector3.zero, dir, Color.black) / rotate around a point specified angle / self-coordinate / rotation center / around rotation axis / rotation angle / public Vector3 RotateRound (Vector3 position, Vector3 center, Vector3 axis, float angle) {return Quaternion.AngleAxis (angle, axis) * (position-center) + center;}
This is how the Unity vector shared by the editor rotates according to a certain point. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.