📝 24.04.09
어제 기술 면접 문답을 많이 해서 오늘은 생략한다.
오늘 면접 연습을 하면서 여러 피드백을 받았다.
위의 피드백을 바탕으로 기술 면접 문답을 더 정교하게 준비해야겠다.
아이템 드랍 효과 📝
위의 포스트를 참고하여 구현하였는데, Update() 사용이 굳이 필요없을 뿐더러 탑다운 게임을 기반으로 만든 위의 스크립트를 플랫포머 게임 기준으로 새로 구현하였다.
또한, 그림자 효과는 필요없으므로 삭제했다.
using System.Collections;
using UnityEngine;
public class Gem : MonoBehaviour
{
[SerializeField] private int xForce = 5;
[SerializeField] private int yForce = 15;
[SerializeField] private int gravity = 25;
private Vector2 direction;
private bool isGrounded = true;
private float maxHeight;
private float currentHeight;
private void Start()
{
currentHeight = Random.Range(yForce - 1, yForce);
maxHeight = currentHeight;
direction = new Vector2(Random.Range(-xForce, xForce), 0);
Initialize(direction);
StartCoroutine(Move());
}
private IEnumerator Move()
{
while (!isGrounded)
{
currentHeight += -gravity * Time.deltaTime;
transform.position += new Vector3(0, currentHeight, 0) * Time.deltaTime;
transform.position += (Vector3)direction * Time.deltaTime;
yield return null;
CheckGroundHit();
}
}
private void Initialize(Vector2 _direction)
{
isGrounded = false;
maxHeight /= 1.5f;
direction = _direction;
currentHeight = maxHeight;
}
private void CheckGroundHit()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 0.5f, LayerMask.GetMask("Ground"));
Debug.Log(hit.collider);
if (hit.collider != null)
{
// 땅에 닿음
isGrounded = true;
transform.position = hit.point;
}
}
}
땅에 바로 떨어지길 바래서 여러번 튀지 않게 했고, 몬스터가 죽으면 터지는 것처럼 포물선을 그리는 효과만 유지했다.
굳이 꾸준한 이동을 프레임마다 호출할 이유가 없으므로 Update() 대신 코루틴을 통해 이동을 실행하도록 수정했다.