当前位置:网站首页>[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 .
边栏推荐
- Leetcode-543. Diameter of Binary Tree
- @component(““)
- [2022 ACTF]web题目复现
- After the interview, the interviewer roast in the circle of friends
- 按键精灵采集学习-矿药采集及跑图
- Leetcode sword finger offer brush questions - day 20
- Outsourcing for four years, abandoned
- gslx680触摸屏驱动源码码分析(gslX680.c)
- 【性能压测】如何做好性能压测?
- Interviewer: what development models do you know?
猜你喜欢
nacos
[2022 ACTF]web题目复现
Dynamics CRM server deployment - restore database prompt: the database is in use
UWB learning 1
[P2P] local packet capturing
C language (high-level) data storage + Practice
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
leetcode:105. 从前序与中序遍历序列构造二叉树
How can a 35 year old programmer build a technological moat?
Detailed explanation of neo4j installation process
随机推荐
海思芯片(hi3516dv300)uboot镜像生成过程详解
Initial experience of teambiion network disk (Alibaba cloud network disk)
Determining the full type of a variable
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
微博发布案例
Live broadcast platform source code, foldable menu bar
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
【Liunx】进程控制和父子进程
今日现货白银操作建议
L'externalisation a duré trois ans.
URP - shaders and materials - simple lit
buuctf misc USB
4、 High performance go language release optimization and landing practice youth training camp notes
二、并发、测试笔记 青训营笔记
按键精灵脚本学习-关于天猫抢红包
2、 Concurrent and test notes youth training camp notes
【斯坦福计网CS144项目】Lab3: TCPSender
[OBS] win capture requires winrt
[Stanford Jiwang cs144 project] lab3: tcpsender
Model application of time series analysis - stock price prediction