我尝试制作一个脚本,在两点之间来回移动对象。但它只是在无限中飞翔。我整个晚上都试图找出问题所在,但我不知道。 这是代码:
using UnityEngine;
public class MovementBetweenPoints : MonoBehaviour {
public Transform[] keyPoints;
public float speed;
private int currentKeyPoint;
// Use this for initialization
void Start ()
{
transform.position = keyPoints[0].position;
currentKeyPoint = 1;
}
// Update is called once per frame
void Update ()
{
if (transform.position == keyPoints[currentKeyPoint].position)
{
currentKeyPoint++;
}
if (currentKeyPoint >= keyPoints.Length)
{
currentKeyPoint = 0;
}
transform.position = Vector3.MoveTowards(transform.position, keyPoints[currentKeyPoint].position, speed * Time.deltaTime);
}
}
最佳答案
您的脚本可以正常工作。您需要确保检查器中的速度设置为大于 0 的值,并且检查器中的关键点数组也包含一些游戏对象,然后就可以开始了
关于c# - 在点之间来回移动 3d 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29833923/