Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在x轴上平滑移动对象?_C#_Unity3d_Game Development - Fatal编程技术网

C# 如何在x轴上平滑移动对象?

C# 如何在x轴上平滑移动对象?,c#,unity3d,game-development,C#,Unity3d,Game Development,我想问一下,我怎样才能将x轴上的物体平滑地左右移动? 我只想在X轴上移动,其中z轴和y轴与移动前保持不变。 我尝试了以下代码: rb.Transform.TranslateVector3.right*Time.deltatime*130 但这传送了我想要的球员移动速度有多快。 我只想添加x轴,例如,如果对象处于打开状态 0->X 0->Y 0->Z 我想向右移动,那么x轴需要=4,当我想向左移动时,如果对象向右移动,其坐标为4,0,0,我想在按键向左移动时回到0,0,0。当我

我想问一下,我怎样才能将x轴上的物体平滑地左右移动? 我只想在X轴上移动,其中z轴和y轴与移动前保持不变。 我尝试了以下代码:

rb.Transform.TranslateVector3.right*Time.deltatime*130

但这传送了我想要的球员移动速度有多快。 我只想添加x轴,例如,如果对象处于打开状态

0->X
0->Y
0->Z
我想向右移动,那么x轴需要=4,当我想向左移动时,如果对象向右移动,其坐标为4,0,0,我想在按键向左移动时回到0,0,0。当我想要多留一个字段时,坐标必须是-4,0,0 我希望有人能得到我想要的

编辑:

蓝星是球员的位置,我只想在x轴上平稳地左右移动,YZ保持不变,在图片上是我想移动的方式,我只想平稳地移动,而不是传送

蓝色是球员所在的位置,黄色和绿色的星星是球员需要去的位置,当他去右边的时候,左边的时候,他需要再次站在同一个位置上。。。谢谢

试试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);
    }
}

来源:

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

我不知道如何在我的代码中实现这一点,不知道在哪里定义它在x轴上移动多少,以及在哪里定义我不改变Y轴和Z轴,顺便说一句,谢谢你的快速asn如果你想向左移动,你们可以使用:Vector3.left*Time.deltaTime*3f当我这样移动时,它不会移动我需要在x轴上移动多少…当我把较小的数字放到150时,它不会在我需要对象的位置上移动,当移动到右边时,我需要在x=4上移动,当移动到左边时,它会在相同的x位置-4,因为,作为endMarker,您应该设置一个向量,该向量等于开始变换的对象,X除外,X将大于4个单位。假设对象在0开始其行程,您希望在多少秒内进行变换?可能是0,5或1秒。。我在代码中使用了150,因为如果我使用较小的数字,它将无法移动到正确的位置,因此我无法清楚地理解某些内容。此外,此代码传送玩家,其移动不平稳,我无法看到对象向右移动,只是消失并在中间出现在右侧…您能给我看一下此代码示例吗,如果这是可行的,如果物体移动平稳,不能进行远程传送,我会给你钱买啤酒。现在我更明白了,我认为你应该使用Vector3。月球上发布的Lerp解决方案是由奶酪制成的。只需在对象中添加一个名为ExampleClass的C脚本,稍后更改并粘贴代码。您将能够在编辑器中写入公共属性的值。稍后,您将不得不将事件发生绑定到键盘,而不再绑定到开始,但这将使您作为一个工作示例开始。我进行了相关编辑,以在图片中清楚地解释我需要实现的目标。请使用Lerp代码,相信我。但这段代码的作用总是从0开始。您不需要在开始时触发它,而是在击键时触发它。startMarker.position应初始化为object.transform.position。。。和端点标记相同,但右侧或左侧的X 4个单位除外。然后,对象将移动。注:1秒内4个单位表示速度为0.25。此外,记住当距离变小或为零时停止行程,否则更新将继续运行lerp并重新计算位置。