当前位置:网站首页>[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 .
边栏推荐
- 外包干了三年,废了...
- gslx680触摸屏驱动源码码分析(gslX680.c)
- Implementing data dictionary with JSP custom tag
- pytorch 参数初始化
- JS get all date or time stamps between two time stamps
- [Stanford Jiwang cs144 project] lab4: tcpconnection
- Tianqing sends instructions to bypass the secondary verification
- Initial experience of teambiion network disk (Alibaba cloud network disk)
- 解决问题:Unable to connect to Redis
- 智联+影音,AITO问界M7想干翻的不止理想One
猜你喜欢
Jenkins remote build project timeout problem
L'externalisation a duré trois ans.
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
Redis data migration
resource 创建包方式
【webrtc】m98 screen和window采集
Dynamics CRM server deployment - restore database prompt: the database is in use
UWB learning 1
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Convolutional neural network -- understanding of pooling
随机推荐
@component(““)
Six methods of flattening arrays with JS
抽絲剝繭C語言(高階)數據的儲存+練習
Outlier detection technology of time series data
gatk4中的interval是什么??
Outsourcing for four years, abandoned
Invalid table alias or column reference`xxx`
【斯坦福计网CS144项目】Lab3: TCPSender
What are the positions of communication equipment manufacturers?
Gslx680 touch screen driver source code analysis (gslx680. C)
Bi she - college student part-time platform system based on SSM
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
07_ Handout on the essence and practical skills of text measurement and geometric transformation
misc ez_ usb
微博发布案例
微信小程序中的路由跳转
外包干了三年,废了...
The metauniverse of the platofarm farm continues to expand, with Dao governance as the core
Advanced practice of C language (high level) pointer