unity 间隔创造物体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemycreat : MonoBehaviour {


    public Transform enemy;
    public float Cooldown;
    // Use this for initialization
    void Start()
    {
        Cooldown = 0f;
    }

    // Update is called once per frame
    void Update()
    {
        if (Cooldown > 0)
        {
            Cooldown -= Time.deltaTime;
        }
        creat();
    }

    public void creat()
    {
        if (Cancreat)
        {
            var enemyform = Instantiate(enemy) as Transform;
            enemyform.position = new Vector2(12.8f,8.5f);
            Cooldown = 2f;
        }
    }
    public bool Cancreat
    {
        get
        {
            return Cooldown <= 0f;
        }
    }
}

通过设置间隔时间Cooldown来控制产生时间。

Time.deltaTime

我个人理解是这一帧消耗的时间,这样在update()里面就可以用实际时间来控制物体的产生。

你可能感兴趣的:(unity 间隔创造物体)