1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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<Dust> cacheList = new List<Dust>();
- public static Dust CreateDust(GameObject prefab)
- {
- Dust dust = null;
- if(cacheList.Count > 0)
- {
- dust = cacheList[0];
- cacheList.RemoveAt(0);
- }
- else
- {
- GameObject obj = Instantiate<GameObject>(prefab);
- DontDestroyOnLoad(obj);
- dust = obj.GetComponent<Dust>();
- }
- return dust;
- }
- }
|