📝 24.03.13
박싱과 언박싱에 대하여 설명해주세요.
- (꼬리질문) 박싱, 언박싱을 사용할 때 주의해야 할 점이 있다면 무엇이 있나요?
박싱은 값 타입의 객체를 참조 타입으로 변환하는 작업을, 언박싱은 이 박싱되어있는 참조 타입 객체 복사본을 다시 값 타입으로 돌리는 작업을 말합니다. 박싱과 언박싱은 객체에 대한 복사본을 생성하는 과정에서의 버그 발생을 주의해야 합니다.
오늘 답변이 검색으로 정확하게 파악할 수 없어서 너무 어려웠다. 박싱과 언박싱이라는 단어 자체도 생소한데 공식 문서에서의 답변도 다소 모호하여 이해하고 내 단어로 정리하기 힘들었다.
자기 소개는 처음부터 래퍼토리를 좀 정리해와야 할 것 같다. 내가 게임 클라이언트라는 분야를 위해 무엇을 준비해왔는지를 녹일 수 있는 답변을 생각해봐야겠다.
오늘 가장 많은 노력을 한 것은 좌표로 점프 값을 구현하는 것이다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Vector3 currentVelocity = new Vector3(0, 0, 0);
private Vector3 impact;
public Vector2 velocity;
public bool isGrounded { get; set; } = false;
public Vector3 Movement => impact + Vector3.up * velocity.y;
private void FixedUpdate()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector3.down, 0.2f, LayerMask.GetMask("Ground"));
Debug.DrawRay(transform.position, Vector3.down, Color.green);
if (hit.collider != null)
{
isGrounded = true;
}
else
{
isGrounded = false;
}
if (velocity.y <= 0f && isGrounded)
{
velocity = Vector2.zero;
}
else
{
velocity += (Physics2D.gravity * 0.2f) * Time.fixedDeltaTime;
transform.position += new Vector3(velocity.x, velocity.y);
}
impact = Vector3.SmoothDamp(impact, Vector3.zero, ref currentVelocity, 0.3f);
}
/* 생략 */
public void Jump(float _jumpForce)
{
velocity.y += _jumpForce;
}
}
rigidbody
의 velocity
를 좌표값을 통해 구현하는 방법이다. 사실 아직도 움직임이 부드럽진 않아서 좀 더 손봐야 하겠지만 어쨌든 문제없이 점프가 된다.