我试图制作一个脚本来在两个点之间来回移动对象。但这只是飞速发展。我试图整个晚上都发现问题,但是我很高兴。
这是代码:

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

最佳答案

您的脚本可以正常运行。您需要确保在检查器中将speed设置为大于0的值,并且keypoints数组在检查器中也包含一些游戏对象,因此您可以继续进行

关于c# - 在点之间来回移动3d对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29833923/

10-11 22:36
查看更多