当前位置:网站首页>[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 .
边栏推荐
- 探索Cassandra的去中心化分布式架构
- 抽絲剝繭C語言(高階)數據的儲存+練習
- 二、并发、测试笔记 青训营笔记
- URP - shaders and materials - light shader lit
- 2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct
- Outlier detection technology of time series data
- 外包干了三年,废了...
- Music | cat and mouse -- classic not only plot
- KBU1510-ASEMI电源专用15A整流桥KBU1510
- Write CPU yourself -- Chapter 9 -- learning notes
猜你喜欢

【leetcode】1020. Number of enclaves

After 95, the CV engineer posted the payroll and made up this. It's really fragrant

The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)

四、高性能 Go 语言发行版优化与落地实践 青训营笔记

Summary of customer value model (RFM) technology for data analysis

C language (high-level) data storage + Practice

95后CV工程师晒出工资单,狠补了这个,真香...
![[SUCTF 2019]Game](/img/9c/362117a4bf3a1435ececa288112dfc.png)
[SUCTF 2019]Game

@component(““)

Make a bat file for cleaning system garbage
随机推荐
Six methods of flattening arrays with JS
海思芯片(hi3516dv300)uboot镜像生成过程详解
【obs】win-capture需要winrt
What is the difference between TCP and UDP?
Bi she - college student part-time platform system based on SSM
buuctf misc USB
Determining the full type of a variable
外包干了四年,废了...
【leetcode】1020. Number of enclaves
Tongda injection 0day
A concurrent rule verification implementation
Live broadcast platform source code, foldable menu bar
Music | cat and mouse -- classic not only plot
gatk4中的interval是什么??
【Liunx】进程控制和父子进程
《动手学深度学习》(四) -- 卷积神经网络 CNN
BGP experiment (1)
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
IO stream file
[SUCTF 2019]Game