발사라던가 제작했습니다만 ..
데미지 구현은 모르겠고
큐 효율성을 높여주세요
This commit is contained in:
김도환
2026-04-16 21:14:05 +09:00
parent 65c997406b
commit 4946e16710
15 changed files with 2399 additions and 73 deletions

View File

@@ -4,20 +4,23 @@ using UnityEngine;
public class BulletMovement : MonoBehaviour
{
public float moveSpeed = 10;
public float lifeTime = 2;
private float _currentTime = 0;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
private void OnEnable()
{
_currentTime = lifeTime;
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.up * (Time.deltaTime * moveSpeed));
_currentTime -= Time.deltaTime;
if (_currentTime <= 0)
{
gameObject.SetActive(false);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
}
}

View File

@@ -0,0 +1,31 @@
using UnityEngine;
public class InputManager : MonoBehaviour
{
// 1. 진짜 데이터가 담길 숨겨진 변수
private static InputSystem _inputSystem;
// 2. 외부에서 접근할 프로퍼티
public static InputSystem InputSystem
{
get
{
// 누군가 InputSystem을 찾았는데 없을 때만 새로 생성
_inputSystem ??= new InputSystem();
return _inputSystem;
}
}
// Start에서는 더 이상 new를 할 필요가 없습니다!
// (위의 get에서 자동으로 해주니까요)
private void OnEnable()
{
// 이때 객체가 없으면 자동으로 생성(get 호출)된 후 Enable()이 실행됩니다.
InputSystem.Enable();
}
private void OnDisable()
{
InputSystem.Disable();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 860109aa4ce233645bc8d7fd7091b26b

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6eebf4006a33ccf4385b253ac8a23f35

View File

@@ -0,0 +1,23 @@
using System;
using UnityEngine;
public class LivingEntity : MonoBehaviour
{
public float health;
public void Damage(float amount)
{
health -= amount;
if (health <= 0)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.layer == LayerMask.NameToLayer("Bullet"))
{
Destroy(gameObject);
other.gameObject.SetActive(false);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2108c5487af823a49b94fc909c22f92f

103
Assets/Scripts/Player.cs Normal file
View File

@@ -0,0 +1,103 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
private Camera _camera;
private Queue<GameObject> _bulletPool = new Queue<GameObject>();
public GameObject bulletPrefab;
public int bulletCount = 30;
[Header("연사 설정")]
public float fireRate = 0.1f; // 총알 발사 간격 (0.1초마다 1발)
private float _fireTimer; // 발사 타이머
private bool _isShooting; // 발사 버튼을 누르고 있는지 여부
void Start()
{
_camera = Camera.main;
// 처음 클릭 시 즉시 발사되도록 타이머 초기화
_fireTimer = fireRate;
// 기존의 performed 대신 클릭 시작(started)과 종료(canceled) 이벤트를 연결합니다.
InputManager.InputSystem.UI.Click.started += OnShootStarted;
InputManager.InputSystem.UI.Click.canceled += OnShootCanceled;
for (int i = 0; i < bulletCount; i++)
{
var bullet = Instantiate(bulletPrefab);
_bulletPool.Enqueue(bullet);
bullet.SetActive(false);
}
}
// 마우스를 클릭하기 시작했을 때
private void OnShootStarted(InputAction.CallbackContext obj)
{
_isShooting = true;
}
// 마우스 클릭을 뗐을 때
private void OnShootCanceled(InputAction.CallbackContext obj)
{
_isShooting = false;
}
// InputAction.CallbackContext 매개변수를 제거하여 Update문에서 호출 가능하게 변경
private void Shoot()
{
var peek = _bulletPool.Dequeue();
if (peek.activeInHierarchy)
{
_bulletPool.Enqueue(peek);
peek = Instantiate(bulletPrefab, transform);
}
peek.transform.rotation = transform.rotation;
peek.transform.position = transform.position;
peek.SetActive(true);
_bulletPool.Enqueue(peek);
}
void Update()
{
if (_camera is null)
{
return;
}
if (InputManager.InputSystem is null)
{
return;
}
// 1. 캐릭터 회전 처리
var mousePos = InputManager.InputSystem.Player.Mouse.ReadValue<Vector2>();
var screenToWorldPoint = _camera.ScreenToWorldPoint(mousePos);
screenToWorldPoint.z = 0;
Vector2 direction = screenToWorldPoint - transform.position;
var rad2Deg = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, rad2Deg - 90);
// 2. 연사 처리 로직
_fireTimer += Time.deltaTime; // 타이머는 매 프레임 증가
if (_isShooting && _fireTimer >= fireRate)
{
Shoot();
_fireTimer = 0f; // 발사 후 타이머 초기화
}
}
// 에러나 메모리 누수 방지를 위해 스크립트 파괴 시 이벤트 구독 해제
private void OnDestroy()
{
if (InputManager.InputSystem != null)
{
InputManager.InputSystem.UI.Click.started -= OnShootStarted;
InputManager.InputSystem.UI.Click.canceled -= OnShootCanceled;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 888d5e9b7a6b20447979a287da28b419