오늘은 팀 프로젝트를 시작했다. 내가 맡은 부분은, 플레이어 움직임 등 구현, 도전과제 시스템 구현, 플레이어 커스터마이징 시스템 구현이었다. 오늘은 플레이어 움직임에 중점을 두고 실습을 진행해보았다.
자동 이동, 점프, 더블 점프, 슬라이딩 로직을 구현하였다.
평소보다 움직임 로직이 많기 때문에, 각각의 움직임을 실행할 수 있는 조건을 설정하는 것이 까다로웠다.
BoxCollider 2D 컴포넌트를 붙여주었다.isGrounded = groundDetector.IsTouchingLayers(LayerMask.GetMask("Ground"));isSliding, isJumping 등의 불리언 변수를 이용하여 움직임 여부를 판단했다.isGrounded == false이고 isJumping == true 일 때는 슬라이딩을 할 수 없게 만들기 위해 다음과 같은 방식으로 움직임 조건을 설정하였다. if (Input.GetKey(KeyCode.LeftShift) && isGrounded && !isSliding)
{
StartSlide();
}PlayerCollider와 slidingCollider를 전부 붙여주었다.SlidingCollider는 끄고 PlayerCollider가 켜져있는 상태이고 슬라이딩 할 때는 PlayerCollider를 끄고 SlidingCollider를 켜는 방식으로 구현하였다.다음으로 시간에 따라 속도가 증가하는 로직과 체력이 감소하는 로직을 만들었다.
[SerializeField] private float healthdecreaseAmount = 0.1f; // 체력 감소량
[SerializeField] private float healthdecreaseInterval = 0.1f; // 체력 감소 시간
[SerializeField] private float speedUpInterval = 10f; // 속도 증가 시간
[SerializeField] private float speedUpAmount = 1f; // 속도 증가량
.
.
.
void Start()
{
StartCoroutine(SpeedUp()); // 시간에 따라 속도 증가
StartCoroutine(HpDecrease()); // 시간에 따라 체력 감소
}
.
.
.
private IEnumerator SpeedUp()
{
while (!isDead)
{
yield return new WaitForSeconds(speedUpInterval);
speed += speedUpAmount;
Debug.Log("Speed Up: " + speed); // 속도 증가 사운드, 이펙트, UI 등 추가
}
}
private IEnumerator HpDecrease()
{
while (!isDead)
{
yield return new WaitForSeconds(healthdecreaseInterval);
currenthealth -= healthdecreaseAmount;
Debug.Log("Health Decrease: " + currenthealth); // 체력 감소 이펙트, UI 등 추가
if (currenthealth <= 0)
Die();
}
}
추후 추가할 회복 아이템을 위한 Heal 메서드나 Die 메서드 등을 만들었다.
이후 강의에서 배운 ParticleSystem을 활용한 애니메이션을 만들었다.
도전과제 시스템을 구현하기 위한 기반 작업을 진행하였다.
PlayerController.cs에 업적을 위한 수치들을 설정할 필드를 생성했다. public int damagedTimes; // 데미지 입은 횟수
public int ObstacleCount; // 장애물 수
public int ObstacleComboCount; // 데미지를 입지 않고 넘은 장애물 수
.
.
.
TakeDamage()메서드에서 데미지 입은 횟수나 콤보를 갱신하는 데에 머물렀지만, 추후 추가해나갈 예정이다. public void TakeDamage(float damage)
{
if (isDead) return;
damagedTimes++; // 데미지 입은 횟수 증가
ObstacleComboCount = 0; // 장애물 콤보 초기화
currenthealth -= damage; // 체력 감소
animator.SetTrigger("IsDamage");
if (currenthealth <= 0)
Die();
}
public 접근 제한자를 최대한 사용하지 않고, private, [SerializeField] private을 최대한 사용하도록 했다.PlayerController.cs에서 너무 많은 기능을 담당하고 있다.PlayerController.cs는 플레이어 오브젝트에 움직임만을 담당하고, 따로 체력, 도전과제 진행여부 등의 관리를 담당할 수 있는 스크립트를 만드는 것이 유지/보수에 편리할 것이라 생각한다.PlayerCustom 등의 스크립트를 붙인다.PlayerCustom 스크립트에 Slot 등의 이름을 가진 구조체로 부위 이름과 해당 부위에 붙일 SpriteRenderer를 정의한다.