【源码】计时器,每隔2秒执行一次

using UnityEngine;
using System.Collections;
public class EnemyAttack :MonoBehaviour {
 public float attackTimer;
 public float attackTime;


 void Start ()
 {
  attackTimer = 0;
  attackTime = 2.0f;
 }
 

 void Update ()
 {
  if (attackTimer>0)
   attackTimer-= Time.deltaTime;
  if (attackTimer<0)
   attackTimer=0;
  if(attackTimer == 0)
  {
   Attack();
   attackTimer =attackTime;
  }
 }
 public void Attack()
 {
  //  你要执行的代码 每隔2S执行一次
 }
}


你可能感兴趣的:(【源码】)