当前位置:网站首页>【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()方法。远离的方向向量=圆心位置-小球位置。
边栏推荐
- L'externalisation a duré trois ans.
- 1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration
- Unity C function notes
- Advanced level of C language (high level) pointer
- 深度学习花书+机器学习西瓜书电子版我找到了
- Torefs API and toref API
- KBU1510-ASEMI电源专用15A整流桥KBU1510
- Detailed explanation of transform origin attribute
- Why is the row of SQL_ The ranking returned by number is 1
- Several important steps to light up the display
猜你喜欢

MySQL service is missing from computer service

Outsourcing for three years, abandoned

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?

机器人技术创新与实践旧版本大纲

外包幹了三年,廢了...

记一个并发规则验证实现

mips uclibc 交叉编译ffmpeg,支持 G711A 编解码

resource 创建包方式

Leetcode-226. Invert Binary Tree

2、 Concurrent and test notes youth training camp notes
随机推荐
Procedure in PostgreSQL supports transaction syntax (instance & Analysis)
四、高性能 Go 语言发行版优化与落地实践 青训营笔记
[2022 ACTF]web题目复现
二、并发、测试笔记 青训营笔记
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
1089: highest order of factorial
外包幹了三年,廢了...
深度学习花书+机器学习西瓜书电子版我找到了
Invalid table alias or column reference`xxx`
直播平台源码,可折叠式菜单栏
Differences between H5 architecture and native architecture
Example of Pushlet using handle of Pushlet
Convolutional neural network -- understanding of pooling
Torefs API and toref API
面试官:你都了解哪些开发模型?
07_ Handout on the essence and practical skills of text measurement and geometric transformation
Implementing data dictionary with JSP custom tag
URP - shaders and materials - light shader lit
Write CPU yourself -- Chapter 9 -- learning notes
Make a bat file for cleaning system garbage