当前位置:网站首页>【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()方法。远离的方向向量=圆心位置-小球位置。
边栏推荐
- [2022 ACTF]web题目复现
- 二、并发、测试笔记 青训营笔记
- L'externalisation a duré trois ans.
- Detailed explanation of neo4j installation process
- Write CPU yourself -- Chapter 9 -- learning notes
- gslx680触摸屏驱动源码码分析(gslX680.c)
- 身边35岁程序员如何建立起技术护城河?
- "Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr
- 虚拟机的作用
- 【leetcode】1020. Number of enclaves
猜你喜欢

Music | cat and mouse -- classic not only plot

IPv4 exercises

UWB learning 1

Flexible layout (II)

"Xiaodeng in operation and maintenance" meets the compliance requirements of gdpr

Leetcode-206. Reverse Linked List

Simple example of ros2 planning system plansys2

身边35岁程序员如何建立起技术护城河?

Kuboard can't send email and nail alarm problem is solved

外包幹了三年,廢了...
随机推荐
English translation is too difficult? I wrote two translation scripts with crawler in a rage
$parent (get parent component) and $root (get root component)
Lm11 reconstruction of K-line and construction of timing trading strategy
1090: integer power (multi instance test)
vus.SSR在asynData函数中请求数据的注意事项
leetcode:105. 从前序与中序遍历序列构造二叉树
Tumor immunotherapy research prosci Lag3 antibody solution
Write CPU yourself -- Chapter 9 -- learning notes
Torefs API and toref API
【Liunx】进程控制和父子进程
[cloud native] how to give full play to memory advantage of memory database
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
在线直播系统源码,使用ValueAnimator实现view放大缩小动画效果
JS decorator @decorator learning notes
解决could not find or load the Qt platform plugin “xcb“in ““.
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
Introduction to abnova's in vitro mRNA transcription workflow and capping method
Simple example of ros2 planning system plansys2
About binary cannot express decimals accurately
Interviewer: what development models do you know?