main #13

Closed
heain0122 wants to merge 47 commits from main into DEG-77-튜토리얼-구현
Showing only changes of commit 393b538920 - Show all commits

View File

@ -14,12 +14,60 @@ public struct BulletData
LifeTime = lifeTime; LifeTime = lifeTime;
Speed = speed; Speed = speed;
} }
} }
[RequireComponent(typeof(Collider))]
public class BulletBase : MonoBehaviour public class BulletBase : MonoBehaviour
{ {
private float _speed; // 데이터
private Vector3 _targetPosition; private float _speed = 5f;
private float _damage = 1f;
private float _lifeTime = 10f;
// 내부용
private Vector3 _direction = Vector3.forward;
private float _timer;
public virtual void Initialize(BulletData bulletData)
{
_speed = bulletData.Speed;
_damage = bulletData.Damage;
_lifeTime = bulletData.LifeTime;
// 발사 위치 기준 목표 방향만 계산
_direction = (bulletData.TargetPos - transform.position).normalized;
// 탄환이 바라보는 방향 세팅
transform.rotation = Quaternion.LookRotation(_direction);
_timer = 0f;
}
private void Update()
{
// 1) 이동
transform.position += _direction * (_speed * Time.deltaTime);
// 2) 수명 카운트
_timer += Time.deltaTime;
if (_timer >= _lifeTime)
{
DestroyBullet();
}
}
private void OnTriggerEnter(Collider other)
{
var character = other.GetComponent<CharacterBase>();
if (character != null)
{
character.TakeDamage(_damage);
DestroyBullet();
}
}
protected virtual void DestroyBullet()
{
Debug.Log("## Bullet destroyed");
Destroy(gameObject);
}
} }