将物体向目标移动,但在正弦波中
Unity2d和船,我的意思是太空飞船。因此,我有我想搬到特定目标的船,但是我希望它陷入罪恶之波,所以它上下弹跳,我看了很多YouTube教程,但我仍然不明白如何整合它进入我的代码。
transform.position = Vector2.MoveTowards(transform.position, shipTarget, Time.deltaTime * shipSpeed) ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该脚本解决了问题。
Mathf.sin
函数在周期中旋转带有角运动的变量0
2 * pi -1 之间到1
。将此方法添加到原始位置是关键。只需将其添加到您的对象中,然后在检查员中选择目标即可。您可以移动目标并查看结果。请记住,脚本名称和文件匹配,还要评论是否存在问题。
This script solves the problem. The
Mathf.Sin
function rotates a variable with angular motion in the period0
to2 * pi
between-1
to1
. Adding this method to the original location is the key. Just Add this to your object and select target in the inspector. you can move target and see result.Remember that the script name and the file match, and also comment if there is a problem.
假设太空飞船正在朝方向
变换。位置 - shiptarget
和垂直于直接运动的方向振荡,该方向采用正弦>正弦
wave的路径,飞船的位置
必须在两个方向上进行更新。让我们有一些变量的太空飞船的速度,该变量转化为直线,振幅和
正弦>代码>波,最后是太空飞船的方向。
球员向目标运动:
垂直方向上的玩家移动:
解释一点,
Mathf.sin(float)(float)
将值(在这种情况下,这是自脚本的 start 以来的总时间,即time.time
)。将其乘以频率会影响Sine
在两个连续的 Crests 或槽之间的波浪距离。更多频率,较小的两个连续 rests 和槽之间的距离 。Mathf.sin(float)
达到1
的最大值。同样,Mathf.sin(float) *振幅
达到振幅
的最大值。总结, update 通过在
update()
函数中调用paceship的
转换
。Assuming the spaceship is moving towards the direction
transform.position - shipTarget
and the oscillation in the direction perpendicular to the straight motion which takes the path of asine
wave, theposition
of the spaceship has to update in both the directions.Let's have some variables for the speed of the spaceship that translates into a straight line, the amplitude and frequency of the
sine
wave, and finally the direction of the spaceship.Player movement towards the target:
Player movement in perpendicular direction:
Explaining it a bit,
Mathf.Sin(float)
takes in a floating-point value (which in this case is the total time elapsed since the start of the script, i.e.Time.time
) in radians. Multiplying it with a frequency affects thesine
wave's distance between two consecutive crests or troughs. More the frequency, less the distance between two consecutive crests and troughs.Mathf.Sin(float)
reaches a maximum value of1
. Likewise,Mathf.Sin(float) * amplitude
reaches a maximum value ofamplitude
.Summing up, update the
transform
of the spaceship every frame by calling it in theUpdate()
function.