캐릭터 점프 및 이동 코드 수정
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ea3e7753e5fb28479a6f577e4b35668
|
||||
8
Assets/Scripts/Player.meta
Normal file
8
Assets/Scripts/Player.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 72ed9ef5cc3e0834b911c392c618dccd
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
59
Assets/Scripts/Player/PlayerMove.cs
Normal file
59
Assets/Scripts/Player/PlayerMove.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Player/PlayerMove.cs.meta
Normal file
2
Assets/Scripts/Player/PlayerMove.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0371ecf29219fdd45a4b084bb00968b3
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7faac9c66ceb4b342b90f97ea09a456c
|
||||
Reference in New Issue
Block a user