当前位置:网站首页>[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 .
边栏推荐
- The metauniverse of the platofarm farm continues to expand, with Dao governance as the core
- IPv4 exercises
- Model application of time series analysis - stock price prediction
- 解决could not find or load the Qt platform plugin “xcb“in ““.
- buuctf misc USB
- [OBS] win capture requires winrt
- Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
- Initial experience of teambiion network disk (Alibaba cloud network disk)
- English translation is too difficult? I wrote two translation scripts with crawler in a rage
- Jenkins remote build project timeout problem
猜你喜欢

IO stream file

95后CV工程师晒出工资单,狠补了这个,真香...

海思芯片(hi3516dv300)uboot镜像生成过程详解

Leetcode-206. Reverse Linked List

2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct

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

idea添加类注释模板和方法模板

resource 创建包方式
![[Linux] process control and parent-child processes](/img/4c/89f87ee97f0f8e9033b9f0ef46a80d.png)
[Linux] process control and parent-child processes
![[2022 CISCN]初赛 web题目复现](/img/1c/4297379fccde28f76ebe04d085c5a4.png)
[2022 CISCN]初赛 web题目复现
随机推荐
图解GPT3的工作原理
Write CPU yourself -- Chapter 9 -- learning notes
测试周期被压缩?教你9个方法去应对
[performance pressure test] how to do a good job of performance pressure test?
[OBS] win capture requires winrt
Music | cat and mouse -- classic not only plot
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
毕设-基于SSM大学生兼职平台系统
Why is the row of SQL_ The ranking returned by number is 1
直播平台源码,可折叠式菜单栏
Outsourcing for three years, abandoned
Bi she - college student part-time platform system based on SSM
2、 Concurrent and test notes youth training camp notes
Resource create package method
[cloud native] how to give full play to memory advantage of memory database
1140_ SiCp learning notes_ Use Newton's method to solve the square root
外包干了三年,废了...
My ideal software tester development status
Stockage et pratique des données en langage C (haut niveau)
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)