如何让物体不断掉落

How to let objects fall down continually

开始学习Unity,想让物体掉下来收集起来。到目前为止,我只有一个物体,它只掉了一次。我如何放入循环并让它一遍又一遍地掉落?

public class AppleScript : MonoBehaviour
{
    public float fallSpeed = 8.0f;

    void Start()
    {
        transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);
    }

    void Update()
    {
        transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);
    }
}

如果我没理解错的话,你想让一个物体掉下来然后传送到它的起始位置然后再掉下来。让我们在代码中做到这一点!

    public class AppleScript : MonoBehaviour
    {
        public float fallSpeed = 8.0f;

        //Variables for starting position and length until reset
        private Vector3 _startingPos;
        public float FallDistance = 5f;

        void Start()
        {
            transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);
            // Save starting position
            _startingPos = transform.position;
        }

        void Update()
        {
            transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);

            // If the object has fallen longer than 
            // Starting height + FallDistance from its start position
            if (transform.position.y > _startingPos.y + FallDistance) {
                transform.position = _startingPos;
            }
        }
    }