-
작업 진행 중인 영상 using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] private Rigidbody2D playerRigidbdy2D; [SerializeField] private float walkSpeed = 20.0f; private float xAxis = 0f; private void Update() { GetInputs(); MovePlayer(); } private void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); } private void MovePlayer() { playerRigidbdy2D.velocity = new Vector2(walkSpeed * xAxis, playerRigidbdy2D.velocity.y); if (playerRigidbdy2D.velocity.x > 0) { transform.localScale = new Vector2(1, transform.localScale.y); } else if (playerRigidbdy2D.velocity.x < 0) { transform.localScale = new Vector2(-1, transform.localScale.y); } } }
플레이어 이동을 담당할 PlayerMovement 스크립트 입니다.
이동 구현은 Rigidbody 로 구현이 되어 있으며 사용자의 입력값을 받는 GetInputs() 함수와 입력값을 토대로 실제 플레이어를 이동 시킬 MovePlayer() 함수를 Update() 함수에서 실행 시켜 주었습니다.
*Horizontal은 사용자의 a,d 와 방향키 Left, Right의 입력값을 받아 오며 GetAxisRaw 함수는 -1, 0, 1 세가지 값으로 즉각적인 움직임을 구현할때 사용 됩니다.
*GetAxisRaw에서 "Raw"를 뺀 GetAxis 함수는 -1 ~ 0 ~ 1 의 값을 사용할 수 있습니다.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerJump : MonoBehaviour { [SerializeField] private Rigidbody2D playerRigidbdy2D; [SerializeField] private Transform layPoint; [SerializeField] private LayerMask layerMask; [SerializeField] private float layerYLength = 0.2f; [SerializeField] private float layerXLength = 0.5f; [SerializeField] private float jumpForce = 1.0f; private void Update() { JumpPlayer(); } public bool CheckGround() { if (Physics2D.Raycast(layPoint.position, Vector2.down, layerYLength, layerMask) || Physics2D.Raycast(layPoint.position + new Vector3(layerXLength, 0, 0), Vector2.down, layerYLength, layerMask) || Physics2D.Raycast(layPoint.position + new Vector3(-layerXLength, 0, 0), Vector2.down, layerYLength, layerMask)) { return true; } else { return false; } } private void JumpPlayer() { if (Input.GetKeyDown(KeyCode.Space) && CheckGround()) { playerRigidbdy2D.velocity = new Vector3(playerRigidbdy2D.velocity.x, jumpForce); } } private void OnDrawGizmos() { Gizmos.color = Color.red; // 현재 오브젝트와 타겟 오브젝트 사이에 선 그리기 Gizmos.DrawLine(layPoint.position, layPoint.position + new Vector3(layerXLength, 0, 0)); Gizmos.DrawLine(layPoint.position, layPoint.position + new Vector3(-layerXLength, 0, 0)); } }
플레이어의 점프를 담당할 PlayerJump 스크립트 입니다.
플레이어의 점프도 이동 로직과 마찬가지로 Rigidbody 로 구현이 되어있으며 점프를 하기 전 점프를 할 수 있는 상태인지(플레이어가 그라운드와 맞닿아 있는지) 판단하기 위해 CheckGround() 함수에서 레이를 쏴 Bool 값으로 받아 왔으며 JumpPlayer 함수 내 점프키인 "Space"를 누를때 true || false를 같이 판단 합니다.
*OnDrawGizmos() 함수는 에디터에서 시각적으로 확인하기 위한 용도의 함수 입니다.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerAnimations : MonoBehaviour { [SerializeField] private PlayerJump playerJump; [SerializeField] private Animator playerAnimator; [SerializeField] private Rigidbody2D playerRigidbody2D; private void Update() { PlayerWalking(); PlayerJumping(); } private void PlayerWalking() { playerAnimator.SetBool("Walk", playerRigidbody2D.velocity.x != 0 && playerJump.CheckGround()); } private void PlayerJumping() { playerAnimator.SetBool("Jump", !playerJump.CheckGround()); } }
플레이어의 애니메이션을 담당하는 PlayerAnimations 스크립트 입니다.
플레이어의 걷기 애니메이션을 실행하기 위한 조건은 플레이어의 Rigidbody 의 속도(x축)이 0이 아닐시 입니다.
x축 (빨강), y축(녹색) 플레이어의 점프 애니메이션을 실행하기 위한 조건은 플레이어가 그라운드와 맞닿아 있는지 판단하는 CheckGround() 함수로 조건을 걸어 주었습니다.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollowPlayer : MonoBehaviour { [SerializeField] private Transform player; [SerializeField] private Vector3 cameraOffset = new Vector3(0, 0, -10); private Vector3 velocity = Vector3.zero; private void LateUpdate() { transform.position = Vector3.SmoothDamp(transform.position, player.position + cameraOffset, ref velocity, 0.1f); } }
카메라가 플레이어를 따라다닐 구현을 하게 될 CameraFolowPlayer 스크립트 입니다.
플레이어의 좌표 정보를 받아와 맵핑을 하는것 보단 SmoothDamp 함수를 사용하여 부드러운 감속을 구현해 주었습니다.
*SmoothDamp 함수는 인자로 A 가 b 의 위치 까지 도달함에 있어서 가까워 질수록 천천히 멈춥니다.
마지막으로
플레이어의 에니메이션 컨트롤러 입니다. 파라미터로 Bool 변수인 "Walk" 와 "Idle" 를 생성하여 각 True || False 에 맞는 조건을 걸어 주었습니다.
오늘은 이만 마치며 다음 글엔 좀 더 진행된 모습으로 찾아 뵙겠습니다.
감사합니다.
'연습 프로젝트 > 2D로그라이크 게임' 카테고리의 다른 글
UI 정보 패널 & 미니맵 (0) 2024.06.08 UI 기획 (1차) (0) 2024.06.08 몬스터 상태 구현 (Update) (0) 2024.06.08 몬스터 상태 구현 (0) 2024.06.07 플레이어 공격 (0) 2024.06.07