当前位置:网站首页>【Unity】物体做圆周运动的几个思路
【Unity】物体做圆周运动的几个思路
2022-07-07 04:10:00 【煮粥侠_99】
第一种
没有数学基础的情况下,直接使用Unity提供的API:
只需要在Update里面放一行代码
this.transform.RotateAround(targetTrans.position, Vector3.forward, 180 * Time.deltaTime);
// targetTrans.position 是圆心的位置,这里我用一个空物体,可以可视化编辑圆心的位置。
// Vector3.forward是旋转轴,因为是2D所以我使用z轴。
// 180表示每秒钟旋转180度也就是半圈儿。
我们可以在2D场景里放置一个小球用于观测运动,在小球上附加此脚本。 再放一个空物体,设置为targetTrans对象。它表示圆心的位置。
小球会围绕圆心做匀速圆周运动。
第二种
思路:使用 Quaternion.AngleAxis()旋转向量的方法,想象一块表盘,秒针旋转360度,它的末端就会画出一个圆。
那么只需要具备“圆心”(位置)、“半径”(向量),两者相加,就可以得到圆上点的位置。
持续旋转这个“半径”,得到圆周运动。
Vector3 CenterPosition = Vector3.zero; //圆心的位置,我设定在(0,0)点
Vector3 r; //圆半径,也就是要旋转的向量。
private void Awake()
{
r = transform.position - CenterPosition; //圆心指向“我”的向量,就是半径
}
public void Update()
{
//以每秒180度的速度旋转“半径”向量,因为我做2D游戏,所以旋转轴是Z轴。
r = Quaternion.AngleAxis(180 * Time.deltaTime, Vector3.forward) * r;
//圆心位置 + 半径 = 圆上的点
transform.position = CenterPosition + r;
}第三种
数学的方法,使用三角函数。可以找到很多参考,就不上代码了
使用的重要函数:
- Mathf.Sin () 获得一个角度的sin值,参数使用弧度
- Mathf.Cos() 同上,cos值。
- 可能会用到:Mathf.Deg2Rad 度转化为弧度,供以上两个函数使用
- 关于弧度
原理:在坐标系原点,将一个 单位向量 从X轴的正方向 向上旋转α度,得到单位向量在圆上的交叉点P(x,y),
三角函数告诉我们,cos (α) = x / 1 = x;sin (α) = y / 1 = y。 反过来 x = cos (α) , y = sin(α).
我们就可以得到 P 的坐标 ( cos(α) , sin (α) )
至此,由旋转的角度可以获得圆上点坐标。
具体实现可参考UNITY 围绕一个物体做圆周运动_Black-Coder的博客-CSDN博客_unity 圆周运动

一个联想

一个简单的做螺旋运动的思路:圆周运动时,半径匀速增长就可以了。
用第一种圆周运动的方法举例, 将圆心targetTrans每帧向着远离“小球”的方向移动。用transform.Translate()方法。远离的方向向量=圆心位置-小球位置。
边栏推荐
- Software acceptance test
- Chinese and English instructions prosci LAG-3 recombinant protein
- Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
- 外包干了三年,废了...
- Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met
- gatk4中的interval是什么??
- 抽丝剥茧C语言(高阶)指针进阶练习
- A concurrent rule verification implementation
- Simple example of ros2 planning system plansys2
- ../ And/
猜你喜欢

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

How to reduce inventory with high concurrency on the Internet

Leetcode-226. Invert Binary Tree

A concurrent rule verification implementation

About binary cannot express decimals accurately

IO流 file

Jenkins远程构建项目超时的问题

科技云报道:从Robot到Cobot,人机共融正在开创一个时代

L'étape avancée du pointeur de langage C (haut de gamme) pour l'enroulement des cocons

Example of Pushlet using handle of Pushlet
随机推荐
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
IPv4 exercises
Le Service MySQL manque dans le service informatique
resource 创建包方式
Leetcode-226. Invert Binary Tree
Abnova immunohistochemical service solution
解决:Could NOT find KF5 (missing: CoreAddons DBusAddons DocTools XmlGui)
抽絲剝繭C語言(高階)指針的進階
Leetcode-543. Diameter of Binary Tree
Kuboard can't send email and nail alarm problem is solved
Dynamics CRM server deployment - restore database prompt: the database is in use
URP - shaders and materials - light shader lit
Chinese and English instructions prosci LAG-3 recombinant protein
抽丝剥茧C语言(高阶)指针的进阶
4、 High performance go language release optimization and landing practice youth training camp notes
IO流 file
Convolutional neural network -- understanding of pooling
1090: integer power (multi instance test)
2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct
3、 High quality programming and performance tuning practical youth training camp notes