当前位置:网站首页>[unity] several ideas about circular motion of objects
[unity] several ideas about circular motion of objects
2022-07-07 07:44:00 【Porridge cook_ ninety-nine】
The first one is
Without the foundation of Mathematics , Use it directly Unity Provided API:
Transform.RotateAround Move around . The object surrounded is the center of the circle , If you don't understand, you can check Manual links
Only need Update Put a line of code inside
this.transform.RotateAround(targetTrans.position, Vector3.forward, 180 * Time.deltaTime);
// targetTrans.position Is the position of the center of the circle , Here I use an empty object , You can visually edit the position of the center of the circle .
// Vector3.forward Is the axis of rotation , the reason being that 2D So I use z Axis .
// 180 Indicates rotation per second 180 Degree is half a circle .
We can do it in 2D Place a small ball in the scene to observe the movement , Attach this script to the ball . Put another empty object , Set to targetTrans object . It indicates the position of the center of the circle .
The ball will make a uniform circular motion around the center of the circle .
The second kind
Ideas : Use Quaternion.AngleAxis() The method of rotating a vector , Imagine a dial , The second hand rotates 360 degree , It will draw a circle at the end .
Then you only need to have “ center of a circle ”( Location )、“ radius ”( vector ), Add the two together , You can get the position of the point on the circle .
Keep rotating this “ radius ”, Get circular motion .
Vector3 CenterPosition = Vector3.zero; // The position of the center , I set it at (0,0) spot
Vector3 r; // Circle radius , That is, the vector to rotate .
private void Awake()
{
r = transform.position - CenterPosition; // The center of the circle points “ I ” Vector , That's the radius
}
public void Update()
{
// Per second 180 The speed of degrees “ radius ” vector , Because I do 2D game , So the axis of rotation is Z Axis .
r = Quaternion.AngleAxis(180 * Time.deltaTime, Vector3.forward) * r;
// Center position + radius = Points on the circle
transform.position = CenterPosition + r;
}The third kind of
Mathematical methods , Using trigonometric functions . You can find many references , I'm not going to go into the code
Important functions used :
- Mathf.Sin () Get an angle sin value , Parameters use radians
- Mathf.Cos() ditto ,cos value .
- May be used :Mathf.Deg2Rad Degrees to radians , For the above two functions
- On radians
principle : At the origin of the coordinate system , Will a Unit vector from X The positive direction of the axis Rotate up α degree , obtain Unit vector At the intersection of the circle P(x,y),
Trigonometric functions tell us ,cos (α) = x / 1 = x;sin (α) = y / 1 = y. In turn, x = cos (α) , y = sin(α).
We can get it P Coordinates of ( cos(α) , sin (α) )
thus , The coordinates of points on the circle can be obtained from the rotation angle .
Specific implementation can refer to UNITY Make circular motion around an object _Black-Coder The blog of -CSDN Blog _unity Circular motion

An association

A simple idea of doing spiral motion : In circular motion , Just increase the radius at a uniform speed .
Take the first circular motion method for example , Center of circle targetTrans Every frame moves away “ Pellet ” Direction of movement . use transform.Translate() Method . Direction vector away = Center position - Ball position .
边栏推荐
- Detailed explanation of neo4j installation process
- [Stanford Jiwang cs144 project] lab3: tcpsender
- [semantic segmentation] - multi-scale attention
- [UTCTF2020]file header
- IPv4 exercises
- numpy中dot函数使用与解析
- Solve could not find or load the QT platform plugin "xcb" in "
- Tongda injection 0day
- buuctf misc USB
- [webrtc] m98 Screen and Window Collection
猜你喜欢

Talk about seven ways to realize asynchronous programming

Interviewer: what development models do you know?

A concurrent rule verification implementation

Initial experience of teambiion network disk (Alibaba cloud network disk)

Outsourcing for three years, abandoned

外包干了三年,废了...

My ideal software tester development status

leetcode:105. 从前序与中序遍历序列构造二叉树
![[Linux] process control and parent-child processes](/img/4c/89f87ee97f0f8e9033b9f0ef46a80d.png)
[Linux] process control and parent-child processes

After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
随机推荐
Implementing data dictionary with JSP custom tag
About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model
My ideal software tester development status
How to reduce inventory with high concurrency on the Internet
buuctf misc USB
Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
Convolutional neural network -- understanding of pooling
[UTCTF2020]file header
Outlier detection technology of time series data
Jenkins远程构建项目超时的问题
What is the difference between TCP and UDP?
@component(““)
C language (high-level) data storage + Practice
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
【webrtc】m98 screen和window采集
IPv4 exercises
知识点滴 - 关于苹果认证MFI
leetcode:105. 从前序与中序遍历序列构造二叉树
[ANSYS] learning experience of APDL finite element analysis