149 lines
4.8 KiB
C#
149 lines
4.8 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
public float moveSpeed = 5f; // 이동 속도
|
|
public float dashDistance = 3f; // 대시 거리
|
|
public float dashCooldown = 1f; // 대시 쿨타임
|
|
public float maxSpeed = 8f; // 최대 속도 (2~3초 후 최대 속도 도달)
|
|
public float accelerationTime = 2f; // 2초 동안 가속
|
|
private float moveTime = 0f; // 이동한 시간
|
|
public float dashTime = 0.5f;
|
|
private bool canDash = true; // 대시 가능 여부
|
|
private Vector2 moveDirection = Vector2.zero; // 현재 이동 방향
|
|
private bool isWalking = false; // 이동 중인지 여부
|
|
private float dashValue;
|
|
Animator animator;
|
|
SpriteRenderer spriteRenderer;
|
|
|
|
private float idleState = 0f; // 0: 오른쪽, 0.333: 왼쪽, 0.666: 위, 1: 아래
|
|
|
|
void Start()
|
|
{
|
|
spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
|
animator = GetComponentInChildren<Animator>();
|
|
|
|
// 키 이벤트와 스프라이트 및 회전 처리 연결
|
|
PlayManager.Instance.keySdown += () => SetMovement(Vector2.down, 1f / 3f); // 아래
|
|
PlayManager.Instance.keyWdown += () => SetMovement(Vector2.up, 2f / 3f); // 위
|
|
PlayManager.Instance.keyAdown += () => SetMovement(Vector2.left, 1f); // 왼쪽
|
|
PlayManager.Instance.keyDdown += () => SetMovement(Vector2.right, 1f); // 오른쪽
|
|
|
|
// 대시 기능 연결
|
|
PlayManager.Instance.keySpace += Dash;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
HandleMovement(); // 이동 처리
|
|
}
|
|
|
|
// 이동 처리
|
|
void HandleMovement()
|
|
{
|
|
if (!canDash) return;
|
|
moveDirection = Vector2.zero; // 매 프레임 초기화
|
|
|
|
// 키 상태를 직접 확인하고 방향 결정
|
|
if (Input.GetKey(KeyCode.A)) moveDirection += Vector2.left;
|
|
if (Input.GetKey(KeyCode.D)) moveDirection += Vector2.right;
|
|
if (Input.GetKey(KeyCode.W)) moveDirection += Vector2.up;
|
|
if (Input.GetKey(KeyCode.S)) moveDirection += Vector2.down;
|
|
|
|
// 이동 여부 판별
|
|
isWalking = moveDirection != Vector2.zero;
|
|
animator.SetBool("isWalking", isWalking);
|
|
|
|
// 이동 방향이 있을 때만 이동
|
|
if (isWalking)
|
|
{
|
|
moveTime += Time.deltaTime;
|
|
// 2초 이상 이동하면 즉시 속도를 maxSpeed로 증가
|
|
float currentSpeed = HandleAcceleration();
|
|
|
|
transform.position += (Vector3)(moveDirection.normalized * currentSpeed * Time.deltaTime);
|
|
// A 또는 D가 눌린 상태라면 Side 애니메이션을 우선 적용
|
|
if (moveDirection.x != 0)
|
|
{
|
|
animator.SetFloat("moveX", 1); // 무조건 Side 애니메이션
|
|
animator.SetFloat("moveY", 0); // 위/아래는 무시
|
|
}
|
|
else
|
|
{
|
|
animator.SetFloat("moveX", 0);
|
|
animator.SetFloat("moveY", moveDirection.y);
|
|
}
|
|
|
|
// 회전 처리
|
|
if (Mathf.Abs(moveDirection.x) > 0)
|
|
{
|
|
if (moveDirection.x < 0 && transform.GetChild(0).localEulerAngles.y != 180)
|
|
{
|
|
transform.GetChild(0).localEulerAngles = new Vector3(0, 180, 0);
|
|
}
|
|
else if (moveDirection.x > 0 && transform.GetChild(0).localEulerAngles.y != 0)
|
|
{
|
|
transform.GetChild(0).localEulerAngles = new Vector3(0, 0, 0);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 이동하지 않으면 Idle 상태를 설정
|
|
moveTime = 0f;
|
|
animator.SetFloat("moveX", 0);
|
|
animator.SetFloat("moveY", 0);
|
|
animator.SetFloat("idleState", idleState);
|
|
}
|
|
|
|
}
|
|
float HandleAcceleration()
|
|
{
|
|
if (isWalking)
|
|
{
|
|
moveTime += Time.deltaTime;
|
|
if (moveTime >= accelerationTime)
|
|
{
|
|
return maxSpeed; // 2초 이상 이동 시 가속
|
|
}
|
|
}
|
|
return moveSpeed; // 기본 속도
|
|
}
|
|
// 대시 처리
|
|
void Dash()
|
|
{
|
|
if (canDash)
|
|
{
|
|
canDash = false;
|
|
isWalking = false;
|
|
animator.SetTrigger("DodgeTrigger");
|
|
|
|
StartCoroutine(DashCoroutine()); // 대시 시작
|
|
}
|
|
}
|
|
|
|
IEnumerator DashCoroutine()
|
|
{
|
|
Vector3 startPosition = transform.position;
|
|
Vector3 targetPosition = startPosition + (Vector3)(moveDirection.normalized * dashDistance);
|
|
float elapsedTime = 0f;
|
|
|
|
while (elapsedTime < dashTime)
|
|
{
|
|
transform.position = Vector3.Lerp(startPosition, targetPosition, elapsedTime / dashTime);
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
transform.position = targetPosition;
|
|
canDash = true;
|
|
}
|
|
// 이동 방향과 스프라이트, 회전 처리
|
|
void SetMovement(Vector2 direction, float idleStateValue)
|
|
{
|
|
moveDirection = direction;
|
|
idleState = idleStateValue;
|
|
}
|
|
}
|