在 Unity 中随着时间的推移(或旋转)从一个地方移动到另一个地方 [英] Moving from one place to another over time (or rotating) in Unity

查看:32
本文介绍了在 Unity 中随着时间的推移(或旋转)从一个地方移动到另一个地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着时间的推移,我在 Unity 中从一个地方移动到另一个地方时遇到问题.我希望我的角色从 Y 上的当前位置移动到当前 + 1.不幸的是,看起来它没有正确获取当前位置或其他什么,因为如果我调试我写的内容,它说幅度总是 1,所以重点是和我一起移动.它不应该只是检查当前位置并将 1 添加到 Y,移动到该位置然后再次检查吗?我不知道这段代码有什么问题,或者它是否与 Unity 如何实时检查位置和事物密切相关?

I have a problem with moving from one place to another in Unity over time. I would like my character to move from current position to current + 1 on Y. Unfortunately, looks like it does not get the current position properly or something, since if I debug what I wrote, it says that the magnitude is always 1, so the point is moving with me. Shouldn't it just check the current position and add 1 to Y, move to that position and then check again? I have no idea what's wrong with this code, or if it's strictly connected with how Unity checks positions and things in real time?

 public bool moving = false;
 private Vector3 dir;
 void FrontMovement()
 {
     Vector3 movement = new Vector3(-Mathf.Sin(transform.eulerAngles.z * Mathf.PI / 180), Mathf.Cos(transform.eulerAngles.z * Mathf.PI / 180), 0f); // always moving front, even after rotation

     if (moving == false)
     {
         dir = movement - transform.position;
         moving = true;
         return;
     }
     transform.Translate(dir.normalized * Time.deltaTime);
     if(dir.magnitude <= Time.deltaTime)
     {
         Debug.Log("Finished movement");
         moving = false;
     }
 }
 void FixedUpdate()
 {
     Debug.Log(dir.magnitude);
     FrontMovement();
 }

我也想知道如何随着时间的推移进行轮换.

I would also like to know how to do rotations over time.

推荐答案

https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

lerp 也适用于旋转

lerp also works for rotations

// Movement speed in units per second.
public float speed = 1.0F;

// Time when the movement started.
private float startTime;

// Total distance between the markers.
private float journeyLength;

void StartMoving() {
    // Keep a note of the time the movement started.
    startTime = Time.time;

    Vector3 modifiedPosition = transform.position;
    transform.position.y += 1.0f;

    // Calculate the journey length.
    journeyLength = Vector3.Distance(transform.position, modifiedPosition.position);

    moving = true;
}

// Move to the target end position.
void Update()
{
    if (moving) {
    // Distance moved equals elapsed time times speed..
    float distCovered = (Time.time - startTime) * speed;

    // Fraction of journey completed equals current distance divided by total distance.
    float fractionOfJourney = distCovered / journeyLength;

    // Set our position as a fraction of the distance between the markers.
    transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fractionOfJourney);

        if (fractionOfJourney >= 1.0f) {
        moving = false;
        }
    }
}

这篇关于在 Unity 中随着时间的推移(或旋转)从一个地方移动到另一个地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆