【问题标题】:How to move object on x axis smoothly?如何在 x 轴上平滑移动对象?
【发布时间】:2019-06-18 07:27:08
【问题描述】:

我想问我如何才能将 x 轴上的对象平滑地左右移动? 我只想在 X 轴上精确移动,其中 z 和 y 轴将与移动前保持相同。 我尝试了代码:

rb.Transform.Translate(Vector3.right * Time.deltatime * 130);

但这会传送我想要快速移动的玩家我想要多少。 例如,如果对象在

上,我只想在 x 轴上添加
0->X
0->Y
0->Z

我想向右移动,那么 x 轴需要 = 4,当我想向左移动时,如果对象向右移动并且它的坐标 (4,0,0) 我想回到 (0, 0,0) 当按键离开时。当我想要多一个字段时,坐标需要是(-4,0,0) 我希望有人能得到我想要达到的目标。

编辑:

蓝星是玩家在位置上,我只想在 YZ 保持不变的 x 轴上平滑地左右移动,图片上是我想要移动的方式,我只想平滑移动而不是瞬移

enter image description here

蓝色是玩家所在的位置,而黄色和绿色的星星是玩家需要去的地方 什么时候去右边 OFC 什么时候去左边需要再次在同一个位置上......谢谢

【问题讨论】:

  • 您希望翻译在几秒内完成?
  • 可能是 0,5 或 1 秒 .. 我在代码中做了 150 ,因为如果我输入较小的数字,它就不会移动到正确的位置,所以我不清楚某些东西..这个代码也会传送播放器移动不顺畅我看不到物体向右移动只是消失并出现在右侧...

标签: c# unity3d game-development


【解决方案1】:

试试 Vector3.Lerp

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    // Transforms to act as start and end markers for the journey.
    public Transform startMarker;
    public Transform endMarker;

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

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

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

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

        // Calculate the journey length.
        journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
    }

    // Follows the target position like with a spring
    void Update()
    {
        // Distance moved = time * speed.
        float distCovered = (Time.time - startTime) * speed;

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

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

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

【讨论】:

  • 我不知道如何在我的代码中实现这一点以及在哪里定义它将在 x 轴上移动多少以及在哪里定义我不改变 Y 和 Z 轴,顺便说一句谢谢你要快速回答
  • 那么如果你想向左移动,你可以使用: Vector3.left * Time.deltaTime * 3f 当我这样移动时,它不会移动我需要在 x 轴上移动多少.. .
  • 当我输入小于 150 的数字时,它不会移动到我需要对象的位置,当向右移动时,我需要在 X = 4 上,当向左移动时,相同的 x 位置 - 4
  • 因为,作为 endMarker,你应该设置一个向量等于对象开始变换,除了 X,它将大 4 个单位。
  • (假设对象从 0 0 0 开始其旅程)
【解决方案2】:

每一帧,你都可以用原始的 z 和 y 创建一个新的 Vector3,然后根据时间逻辑更新 x。然后将此向量分配给对象变换。这是最简单的方法。

【讨论】:

  • 你能给我看一下这个的代码示例吗?如果可行,如果物体移动平稳而不是传送,我会给你钱买啤酒。
  • 现在我明白了,我觉得你应该用moon贴出的Vector3.Lerp解决方案是奶酪做的。只需将 C# 脚本添加到名为 ExampleClass 的对象(稍后更改)并粘贴代码。您将能够在编辑器中编写公共属性的值。稍后您将不得不将事件的发生与键盘联系起来,而不是再与 Start() 联系起来,但这将使您作为一个工作示例开始。
  • 我对问题进行了编辑,以在图片中清楚地解释我需要实现的目标。
  • 使用 Lerp 代码,相信我。但是该代码的作用始终是从 0 0 0 开始。您不需要在 Start() 上触发它,而是在击键时触发它。并且 startMarker.position 应该被初始化为 object.transform.position ... 并且 endMarker 除了向右或向左 X 4 个单位之外相同。然后对象将移动。注:1秒4个单位表示速度为0.25
  • 此外,当距离变得很小或为零时,请记住停止旅程,否则 update() 将继续运行 lerp 并重新计算位置。
猜你喜欢
  • 1970-01-01
  • 2014-03-15
  • 2019-09-15
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
相关资源
最近更新 更多