Chap10_8.cs
using UnityEngine;
using System.Collections;
public class Chap10_8 : MonoBehaviour {
public int MaxBullet = 20;
Queue bulletBox = new Queue();
ArrayList bulletFired = new ArrayList();
// Use this for initialization
void Start () {
for(int i = 0; i < MaxBullet; i++){
GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
bullet.transform.position = new Vector3(-10, 0, i);
bullet.transform.localScale = Vector3.one * 0.5f;
bullet.AddComponent(typeof(Bullet));
bulletBox.Enqueue(bullet);
}
}
// Update is called once per frame
void Update () {
foreach(GameObject obj in bulletFired){
obj.transform.Translate(0, 0, 5 * Time.deltaTime);
Bullet bulletScript = obj.GetComponent<Bullet>();
if(bulletScript.Life == 0){
bulletFired.Remove(obj);
bulletBox.Enqueue(obj);
obj.transform.position = new Vector3(-10, 0, bulletBox.Count);
break;
}
}
if(Input.GetKeyDown(KeyCode.Space)){
GameObject obj = bulletBox.Dequeue() as GameObject;
obj.transform.position = new Vector3(0, 0, 3);
bulletFired.Add(obj);
obj.SendMessage("Fire");
}
}
}
Bullet.cs
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
public float Life;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Fire(){
Life = 3.0f;
Invoke("Recyle", Life);
}
void Recyle(){
Life = 0.0f;
}
}
'Computer Language > 유니티' 카테고리의 다른 글
문자 짜르기 해쉬 테이블 사용 (0) | 2014.05.20 |
---|---|
해쉬 테이블 -> 어레이 리스트 (0) | 2014.05.20 |
해석중 (0) | 2014.05.20 |
윤석씨 (바둑) (0) | 2014.05.19 |
선생님 예제 입니다. (0) | 2014.05.19 |