아카이브
[개발일지] Undead Survivor 4일차 본문
소환 레벨 적용
근접무기 구현
프리팹을 만든다
Bullet.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float damage;
public int per;
public void Init(float damage, int per)
{
this.damage = damage;
this.per = per;
}
}
충돌로직 작성
Bullet 컴포넌트로 접근하여 데미지를 가져와 피격계산
남은 체력을 조건으로 피격과 사망 나누기
사망할 땐 SetActive 함수를 통한 비활성화
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.Processors;
public class Enemy : MonoBehaviour
{
public float speed;
public float health;
public float maxHealth;
public RuntimeAnimatorController[] animCon;
public Rigidbody2D target;
bool isLive;
Rigidbody2D rigid;
Animator anim;
SpriteRenderer spriter;
// Start is called before the first frame update
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
spriter = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void FixedUpdate()
{
if (!isLive)
return;
Vector2 dirVec = target.position - rigid.position;
Vector2 nextVec = dirVec.normalized * speed * Time.fixedDeltaTime;
rigid.MovePosition(rigid.position + nextVec);
rigid.velocity = Vector2.zero;
}
void LateUpdate()
{
//목표의 x축 값과 자신의 x축값을 비교하여 작으면 true가 되도록 설정
spriter.flipX = target.position.x < rigid.position.x;
}
void OnEnable()
{
target = GameManager.instance.player.GetComponent<Rigidbody2D>();
isLive = true;
health = maxHealth;
}
public void Init(spawnData data)
{
anim.runtimeAnimatorController = animCon[data.spriteType];
speed = data.speed;
maxHealth = data.health;
health = data.health;
}
void OnTriggerEnter2D(Collider2D collision)
{
if (!collision.CompareTag("Bullet")) //매개변수의 태그를 조건으로 사용
return;
health -= collision.GetComponent<Bullet>().damage;
if(health > 0)
{
//Live
}
else
{
//Die
Dead();
}
}
void Dead()
{
gameObject.SetActive(false);
}
}
풀메니저를 써서 근접무기 배치
플레이어에 웨폰 오브젝트 추가,
무기 생성 및 관리를 담당
변수선언
무기 아이디에 따라 로직을 분리할 switch문 작성
업데이트 로직도 switch문 사용하여 무기마다 로직실행
레벨에 따른 배치
배치하며 위치, 회전 초기화
Weapon.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public int id;
public int prefabId;
public float damage;
public int count;
public float speed;
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
switch (id)
{
case 0:
transform.Rotate(Vector3.back * speed * Time.deltaTime);
break;
default:
break;
}
// Test
if (Input.GetButtonDown("Jump"))
{
LevelUp(20, 5);
}
}
public void LevelUp(float damage, int count)
{
this.damage = damage;
this.count += count;
if(id ==0)
Batch();
}
public void Init()
{
switch (id)
{
case 0:
speed = 150;
Batch();
break;
default:
break;
}
}
void Batch()
{
for(int index = 0; index < count; index++)
{
Transform bullet;
if(index < transform.childCount)
{
bullet = transform.GetChild(index); //인덱스가 childCount 범위 내라면 GetChild 함수로 가져오기
}
else
{
//기존 오브젝트 먼저 활용하고 모자란 것은 풀링에서 가져오기
bullet = GameManager.instance.pool.Get(prefabId).transform; //가져온 오브젝트의 Transform을 지역변수로 저장
bullet.parent = transform;
}
bullet.localPosition = Vector3.zero;
bullet.localRotation = Quaternion.identity;
Vector3 rotVec = Vector3.forward * 360 * index / count;
bullet.Rotate(rotVec);
bullet.Translate(bullet.up * 1.5f, Space.World);
bullet.GetComponent<Bullet>().Init(damage, -1); //-1 은 무한으로 관통하는 근접공격
}
}
}
'2D 콘텐츠 제작' 카테고리의 다른 글
2D 프로젝트 개발 발표 (0) | 2023.09.21 |
---|---|
[개발일지] Undead Survivor 5일차 (0) | 2023.09.19 |
[개발일지] Undead Survivor 3일차 (0) | 2023.09.15 |
[개발일지] Undead Survivor 2일차 (0) | 2023.09.14 |
[개발일지] Undead Survivor 1일차 (0) | 2023.09.13 |