using UnityEngine; using System.Collections; using System.Collections.Generic; public class Dust : MonoBehaviour { public Animator animator; private float direction; private float dirSpeed; private float speed; private float speedAcc; private float duration; private float startTime; private bool isHiding; public void Show() { gameObject.SetActive(true); animator.Play("DustShow", 0, 0); direction = Random.Range(0, 2f*Mathf.PI); duration = Random.Range(5f, 10f); startTime = GameTime.time; isHiding = false; Rotate(); } public void Hide() { isHiding = true; animator.Play("DustHide", 0, 0); } public void Remove() { gameObject.SetActive(false); if(!cacheList.Contains(this)) cacheList.Add(this); } // Update is called once per frame void Update () { Vector3 pos = transform.position; pos.x += Mathf.Cos(direction) * speed * GameTime.deltaTime; pos.z += Mathf.Sin(direction) * speed * GameTime.deltaTime; transform.position = pos; direction += dirSpeed*GameTime.deltaTime; speed += 0.01f*GameTime.deltaTime; speed = NumberUtil.forceBetween(speed, 0.5f, 3f); if(Random.Range(0, 100) < 5) speed += Random.Range(-0.5f, 0.5f); if(Random.Range(0, 100) < 5) dirSpeed = Random.Range(-2f, 2f); Rotate(); if(!isHiding && GameTime.time - startTime > duration) Hide(); } private void Rotate() { Vector3 angle = transform.eulerAngles; angle.y = -NumberUtil.radianToAngle(direction); transform.eulerAngles = angle; } private static List cacheList = new List(); public static Dust CreateDust(GameObject prefab) { Dust dust = null; if(cacheList.Count > 0) { dust = cacheList[0]; cacheList.RemoveAt(0); } else { GameObject obj = Instantiate(prefab); DontDestroyOnLoad(obj); dust = obj.GetComponent(); } return dust; } }