using UnityEngine;
using UnityEngine.Pool;
public class BulletManager : MonoBehaviour
{
[SerializeField] GameObject bulletPrefab;
public static BulletManager Instance { get; private set; }
private IObjectPool<Bullet> bulletPool;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
InitializeBulletPool();
}
else if (Instance != this)
{
Destroy(gameObject);
}
}
private void InitializeBulletPool()
{
bulletPool = new ObjectPool<Bullet>(CreateBullet,ActivateBullet, DeactivateBullet,DestroyBullet,false,10,20);
}
private Bullet CreateBullet()
{
var bulletGO = Instantiate(bulletPrefab, transform).GetComponent<Bullet>();
bulletGO.Pool = bulletPool;
bulletGO.gameObject.SetActive(false);
return bulletGO;
}
private void ActivateBullet(Bullet bullet)
{
bullet.gameObject.SetActive(true);
}
private void DeactivateBullet(Bullet bullet)
{
bullet.gameObject.SetActive(false);
}
private void DestroyBullet(Bullet bullet)
{
Destroy(bullet.gameObject);
}
public Bullet GetBullet()
{
return bulletPool.Get();
}
public void ReturnBullet(Bullet bullet)
{
bulletPool.Release(bullet);
}
}
BulletManager.cs (싱글톤)
using UnityEngine;
using UnityEngine.Pool;
public class Bullet : MonoBehaviour
{
public float speed = 10f;
public IObjectPool<Bullet> Pool { get; set; }
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Wall") || other.gameObject.CompareTag("Player"))
{
Pool?.Release(this); // 풀로 반환
}
}
}
Bullet.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] Transform playerPos;
[SerializeField] Transform firePos;
[SerializeField] Transform shoot_bullet_pool;
[SerializeField] int speed = 10;
private void Start()
{
StartCoroutine("LookAtPlayerPos");
StartCoroutine("CoShoot");
}
IEnumerator LookAtPlayerPos()
{
while (true)
{
this.gameObject.transform.LookAt(playerPos);
yield return null;
}
}
IEnumerator CoShoot()
{
while (true)
{
Bullet bullet = BulletManager.Instance.GetBullet();
bullet.transform.SetParent(shoot_bullet_pool);
bullet.transform.position = firePos.position;
bullet.transform.rotation = firePos.rotation;
yield return new WaitForSeconds(1);
}
}
}
Enemy.cs
using Newtonsoft.Json.Bson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player2 : MonoBehaviour
{
public float moveSpeed = 5f;
void Start()
{
StartCoroutine("CoMove");
}
IEnumerator CoMove()
{
while (true)
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(h, 0f, v) * moveSpeed * Time.deltaTime;
transform.Translate(movement);
yield return null;
}
}
}
Player2.cs
'산대특 > 게임 플랫폼 응용 프로그래밍' 카테고리의 다른 글
배경 스크롤링 구현하기 (0) | 2024.03.05 |
---|---|
Quaternion으로 Player 주변에 위성 만들어보기 (0) | 2024.03.05 |
Bullet 발사 시에 오브젝트 풀링으로 Bullet 관리하기 (0) | 2024.03.04 |
A* 알고리즘 구현해보기 (3) - 구해진 경로를 따라 오브젝트 이동 시켜보기 (0) | 2024.03.04 |
A* 알고리즘 구현해보기 (2) - 출발 지점에서 목표 지점까지 경로 구하기 (0) | 2024.03.03 |