当前位置:网站首页>【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()方法。远离的方向向量=圆心位置-小球位置。
边栏推荐
- Robot technology innovation and practice old version outline
- My ideal software tester development status
- Outlier detection technology of time series data
- 1090: integer power (multi instance test)
- JSON introduction and JS parsing JSON
- 1089: highest order of factorial
- MobaXterm
- gslx680触摸屏驱动源码码分析(gslX680.c)
- 修改Jupyter Notebook文件路径
- 微信小程序中使用wx.showToast()进行界面交互
猜你喜欢
身边35岁程序员如何建立起技术护城河?
"Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr
Leetcode-543. Diameter of Binary Tree
Robot technology innovation and practice old version outline
mips uclibc 交叉编译ffmpeg,支持 G711A 编解码
Advanced level of C language (high level) pointer
JSON introduction and JS parsing JSON
07_ Handout on the essence and practical skills of text measurement and geometric transformation
[2022 CISCN]初赛 web题目复现
四、高性能 Go 语言发行版优化与落地实践 青训营笔记
随机推荐
Tencent's one-day life
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
Redis data migration
@component(““)
[semantic segmentation] - multi-scale attention
按键精灵脚本学习-关于天猫抢红包
vus.SSR在asynData函数中请求数据的注意事项
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Procedure in PostgreSQL supports transaction syntax (instance & Analysis)
Advanced practice of C language (high level) pointer
微信小程序中的路由跳转
Flexible layout (II)
JS decorator @decorator learning notes
Bindingexception exception (error reporting) processing
外包干了三年,废了...
微信小程序中使用wx.showToast()进行界面交互
1140_ SiCp learning notes_ Use Newton's method to solve the square root
Fast quantitative, abbkine protein quantitative kit BCA method is coming!
面试结束后,被面试官在朋友圈吐槽了......
"Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr