如何在 x 轴上平滑移动对象?
How to move object on x axis smoothly?
我想请问一下如何在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,什么时候向左,需要再次在同一个位置...谢谢
尝试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
每一帧,您都可以使用对象原始 z 和 y 创建一个新的 Vector3,然后根据时间逻辑更新 x。然后将此向量分配给对象变换。这是最简单的方法。
我想请问一下如何在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,什么时候向左,需要再次在同一个位置...谢谢
尝试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
每一帧,您都可以使用对象原始 z 和 y 创建一个新的 Vector3,然后根据时间逻辑更新 x。然后将此向量分配给对象变换。这是最简单的方法。