【问题标题】:How to start Vector3.Lerp from middle point?如何从中间点开始 Vector3.Lerp?
【发布时间】:2019-05-01 13:16:51
【问题描述】:

我必须将对象从 -10 移动到 +10 x 位置。但是我想从零开始我的对象 x.point 移动,我怎样才能在中间位置开始我的 lerp?

Edit2:对象应该从 x = 0 位置开始,移动到 +10 x 位置,然后转到 -10 x 位置,然后再次 +10 x,-10 x 像一个循环。

Vector3 pos1, pos2, pos0, pos3;
void Start()
{
    pos0 = transform.position;

    pos1 = pos0 + new Vector3(10, 0, 0);

    pos2 = pos0 - new Vector3(10, 0, 0);

    pos3 = pos1;
}
float time = 0.5f;
void FixedUpdate()
{ 
    time += Mathf.PingPong(Time.time * 0.5f, 1.0f);
    transform.position = Vector3.Lerp(pos2, pos1, time);
}

【问题讨论】:

  • 确保x开头为0?它的种类是因为你的东西是从它开始的地方开始的 -10 到 10 .. 你可能不想要 time.time 但 time.deltatime ..
  • transform.position.x 开始时为 0,但当我启动 Lerp(从 -10 到 +10)时,transform.position.x 变为 -10 并开始变为 +10。跨度>
  • 那么只是 Lerp 从 0 到 10?当游戏对象为零时,为什么要尝试从 -10 开始 Lerp?只需将您的 Lerp 调用修改为从 0 到 10 并调整时间以获得您想要的。尝试以这种方式做这件事只是 hacky。
  • 我说它要么像 Tolga 所建议的那样,要么你对 time.time * 0.5 pingponed 的想法,你没有得到你想的值..
  • @BugFinder 我调试了几个小时,但找不到合适的解决方案,只需要帮助解决问题,我没有从别人那里得到好处。

标签: unity3d lerp


【解决方案1】:

来自 https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html 的 Unity API 文档

public static Vector3 Lerp(Vector3 a, Vector3 b, float t);

当 t = 0 时返回 a。当 t = 1 时返回 b。当 t = 0.5 返回 点在 a 和 b 之间。

在 x = 0 正好在起点和终点之间的这种对称情况下,您可以只使用 lerp 和 t 从 t = 0.5 开始。也许是这样的:

Vector3 pos1, pos2, pos0, pos3;

private float t;

void Start()
{
    pos0 = transform.position;
    pos1 = pos0 + new Vector3(10, 0, 0);
    pos2 = pos0 - new Vector3(10, 0, 0);
    pos3 = pos1;

    t = 0.5
}

void FixedUpdate()
{ 
    t += Mathf.PingPong(Time.deltaTime * 0.5f, 1.0f);
    transform.position = Vector3.Lerp(pos2, pos1, t);
}

正如@BugFinder 所指出的,您可能应该使用Time.deltaTime 而不是Time.time

【讨论】:

  • “t”是干什么用的?
  • 用于 Lerp 的时间。现已编辑。
  • 使用您的代码,它在开始时停留在 +10 x 位置。我需要从 0 位置开始我的游戏对象并移动到 +10 并像循环一样再次移动回 -10 和 +10。无论如何,谢谢您的回答,但这不是解决方案:/
  • 好吧,如果他想要一个 PingPong 类型的行为,他仍然可以使用 Time.time 那只是 float t = (Mathf.sin(Time.time) + 1f) / 2; 问题是起始偏移量......
  • @Eddge 你的代码工作正常,但它不是线性的,它在接近目标位置时会减慢,有没有机会让它成为线性而不减慢?
【解决方案2】:

这是我的处理方法:

Vector3 ToPos;
Vector3 FromPos;

void Start()
{
    ToPos = transform.position + new Vector3(10, 0, 0);
    FromPos = transform.position - new Vector3(10, 0, 0);
}

// using Update since you are setting the transform.position directly
// Which makes me think you aren't worried about physics to much.
// Also FixedUpdate can run multiple times per frame.
void Update()
{
    // Using distance so it doesnt matter if it's x only, y only z only or a combination
    float range = Vector3.Distance(FromPos, ToPos);
    float distanceTraveled = Vector3.Distance(FromPos, transform.position);
    // Doing it this way so you character can start at anypoint in the transition...
    float currentRatio = Mathf.Clamp01(distanceTraveled / range + Time.deltaTime);

    transform.position = Vector3.Lerp(FromPos, ToPos, currentRatio);
    if (Mathf.Approximately(currentRatio, 1.0f))
    {
        Vector3 tempSwitch = FromPos;
        FromPos = ToPos;
        ToPos = tempSwitch;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多