게임을 개발하기 위해 유니티에 친숙해지는 시간을 다같이 가졌습니다.
저는 이 시간에 게임의 한 캐릭터를 움직이는 방법에 대해 유튜브로 공부했습니다.
캐릭터를 움직이게 하려면 특정 키가 눌렸을 때 그 캐릭터의 위치 좌표값을 단순히 변경하는 것이 아니라, 그 캐릭터에 적용되는 중력을 변경하는 방식으로 작동하는 것을 배웠습니다.
또, 캐릭터가 움직인다는 것을 묘사하기 위해 애니메이션을 구현하는 방법도 배웠습니다.
using UnityEngine;
using UnityEngine.InputSystem;
public class SpacePlayer : MonoBehaviour
{
Rigidbody2D rb;
[SerializeField] float moveSpeed = 4;
Animator animator;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
}
public void OnMove(InputValue inputValue)
{
float input = inputValue.Get<Vector2>().x;
//Debug.Log("Key : " + input);
if (input > 0)
{
animator.SetBool("Right", true);
animator.SetBool("Left", false);
}
else if (input < 0)
{
animator.SetBool("Left", true);
animator.SetBool("Right", false);
}
if (Mathf.Abs(input) > 0)
{
rb.linearVelocity = input * Vector2.right * moveSpeed;
}
else
{
rb.linearVelocity = Vector2.zero;
animator.SetBool("Left", false);
animator.SetBool("Right", false);
}
}
}