캐릭터 점프 및 이동 코드 수정

This commit is contained in:
Mingu Kim
2025-07-07 21:09:55 +09:00
parent 1f9bd4a693
commit c36f82f485
9 changed files with 75 additions and 42 deletions

View File

@@ -1,14 +0,0 @@
using UnityEngine;
public class Movement2D : MonoBehaviour
{
[SerializeField]
private float moveSpeed = 5f;
public Vector3 MoveDirection { get; set; } = Vector2.zero;
private void Update()
{
transform.position += MoveDirection * moveSpeed * Time.deltaTime;
}
}

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 0ea3e7753e5fb28479a6f577e4b35668

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 72ed9ef5cc3e0834b911c392c618dccd
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,59 @@
using System;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float maxSpeed;
public float jumpPower;
Rigidbody2D rigidBody;
SpriteRenderer spriteRenderer;
// TODO : 걷기, 점프 등 애니메이션
Animator animator;
// 사용하는 컴포넌트들 초기화
void Awake()
{
rigidBody = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}
private void Update()
{
// 점프
if (Input.GetButtonDown("Jump"))
{
rigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
}
// 키 입력 땔때 캐릭터 멈춤
if (Input.GetButtonUp("Horizontal"))
{
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.normalized.x * 0.5f, rigidBody.linearVelocity.y);
}
// 캐릭터(Sprite) 방향
if (Input.GetButtonDown("Horizontal"))
{
spriteRenderer.flipX = Input.GetAxisRaw("Horizontal") >= 1;
}
}
void FixedUpdate()
{
// 캐릭터 움직임 컨트롤
float h = Input.GetAxisRaw("Horizontal");
rigidBody.AddForce(Vector2.right * h, ForceMode2D.Impulse);
// 유니티6 부터 Velocity에서 LinearVelocity로 변경
if (rigidBody.linearVelocity.x > maxSpeed) // 오른쪽 최대 속도
{
rigidBody.linearVelocity = new Vector2(maxSpeed, rigidBody.linearVelocity.y);
}
else if (rigidBody.linearVelocity.x < maxSpeed * (-1))
{
rigidBody.linearVelocity = new Vector2(maxSpeed * (-1), rigidBody.linearVelocity.y);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0371ecf29219fdd45a4b084bb00968b3

View File

@@ -1,19 +0,0 @@
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Movement2D movement2D;
private void Awake()
{
movement2D = GetComponent<Movement2D>();
}
private void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
movement2D.MoveDirection = new Vector2(x, y);
}
}

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 7faac9c66ceb4b342b90f97ea09a456c