将物体向目标移动,但在正弦波中

发布于 2025-01-28 11:10:02 字数 226 浏览 1 评论 0 原文

Unity2d和船,我的意思是太空飞船。因此,我有我想搬到特定目标的船,但是我希望它陷入罪恶之波,所以它上下弹跳,我看了很多YouTube教程,但我仍然不明白如何整合它进入我的代码。

transform.position = Vector2.MoveTowards(transform.position, shipTarget, Time.deltaTime * shipSpeed) ;

unity2d and by ship I mean spaceship. so I have my ship which I want to move to a specific target, but I want it to be in a sin wave so it bounces up and down, I've watched plenty of youtube tutorials but I still do not understand how to integrate it into my code.

transform.position = Vector2.MoveTowards(transform.position, shipTarget, Time.deltaTime * shipSpeed) ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2025-02-04 11:10:02

该脚本解决了问题。 Mathf.sin 函数在周期中旋转带有角运动的变量 0 2 * pi -1 之间到 1 。将此方法添加到原始位置是关键。只需将其添加到您的对象中,然后在检查员中选择目标即可。您可以移动目标并查看结果。

public class SinusMove : MonoBehaviour
{
    private float cycle; // This variable increases with time and allows the sine to produce numbers between -1 and 1.
    private Vector3 basePosition; // This variable maintains the location of the object without applying sine changes

    public Transform target;
    
    public float waveSpeed = 1f; // Higher make the wave faster
    public float bonusHeight = 1f; // Set higher if you want more wave intensity
    public float speed = 1f; // more value going faster to target
   
    public void Start() => basePosition = transform.position;

    void Update()
    {
        cycle += Time.deltaTime*waveSpeed;
    
        transform.position = basePosition + (Vector3.up*bonusHeight) * Mathf.Sin(cycle);
    
        if (target) basePosition = Vector3.MoveTowards(basePosition, target.position, Time.deltaTime * speed);
    }
}

请记住,脚本名称和文件匹配,还要评论是否存在问题。

This script solves the problem. The Mathf.Sin function rotates a variable with angular motion in the period 0 to 2 * pi between -1 to 1. 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.

public class SinusMove : MonoBehaviour
{
    private float cycle; // This variable increases with time and allows the sine to produce numbers between -1 and 1.
    private Vector3 basePosition; // This variable maintains the location of the object without applying sine changes

    public Transform target;
    
    public float waveSpeed = 1f; // Higher make the wave faster
    public float bonusHeight = 1f; // Set higher if you want more wave intensity
    public float speed = 1f; // more value going faster to target
   
    public void Start() => basePosition = transform.position;

    void Update()
    {
        cycle += Time.deltaTime*waveSpeed;
    
        transform.position = basePosition + (Vector3.up*bonusHeight) * Mathf.Sin(cycle);
    
        if (target) basePosition = Vector3.MoveTowards(basePosition, target.position, Time.deltaTime * speed);
    }
}

Remember that the script name and the file match, and also comment if there is a problem.

enter image description here

梦境 2025-02-04 11:10:02

假设太空飞船正在朝方向变换。位置 - shiptarget 和垂直于直接运动的方向振荡,该方向采用正弦>正弦 wave的路径,飞船的位置必须在两个方向上进行更新。

让我们有一些变量的太空飞船的速度,该变量转化为直线,振幅 正弦>代码>波,最后是太空飞船的方向。

private float speed = 1f;
private float amplitude = 1f;
private float frequency = 1f;
private Vector2 straightDirection = (transform.position - shipTarget).normalized; // magnitude = 1
private Vector2 perpendicularDirection = GetPerpendicularDirection(straightDirection);

Vector2 GetPerpendicularDirection(Vector2 direction) {
    return new Vector2(-direction.y, direction.x);
}

球员向目标运动:

Vector2 horizontal = straightDirection * speed * Time.deltaTime;

垂直方向上的玩家移动

Vector2 vertical = perpendicularDirection * Mathf.Sin(frequency * Time.time) * amplitude;

解释一点, Mathf.sin(float)(float)将值(在这种情况下,这是自脚本的 start 以来的总时间,即 time.time )。将其乘以频率会影响 Sine 在两个连续的 Crests 槽之间的波浪距离。更多频率,较小的两个连续 rests 槽之间的距离 Mathf.sin(float)达到 1 的最大值。同样, Mathf.sin(float) *振幅达到振幅的最大值。


总结, update 通过在 update()函数中调用 paceship的转换

private void Update() {
    transform.position = horizontal + vertical;
}

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 a sine wave, the position 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.

private float speed = 1f;
private float amplitude = 1f;
private float frequency = 1f;
private Vector2 straightDirection = (transform.position - shipTarget).normalized; // magnitude = 1
private Vector2 perpendicularDirection = GetPerpendicularDirection(straightDirection);

Vector2 GetPerpendicularDirection(Vector2 direction) {
    return new Vector2(-direction.y, direction.x);
}

Player movement towards the target:

Vector2 horizontal = straightDirection * speed * Time.deltaTime;

Player movement in perpendicular direction:

Vector2 vertical = perpendicularDirection * Mathf.Sin(frequency * Time.time) * amplitude;

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 the sine 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 of 1. Likewise, Mathf.Sin(float) * amplitude reaches a maximum value of amplitude.


Summing up, update the transform of the spaceship every frame by calling it in the Update() function.

private void Update() {
    transform.position = horizontal + vertical;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文