using System.Collections; using System.Collections.Generic; 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ÃÊ µ¿¾È °¡¼Ó public float dashTime = 0.5f; public float moveTime = 0f; // À̵¿ÇÑ ½Ã°£ public enum PlayerState { Idle, Walking, Dash, Attack } public PlayerState playerState; private Vector2 moveDirection = Vector2.zero; // ÇöÀç À̵¿ ¹æÇâ private string[] anim_Strings = { "PosX", "PosY", "idleState" }; PlayerAttack playerAttack; Animator animator; public float idleState = 0f; // 0: ¿À¸¥ÂÊ, 0.333: ¿ÞÂÊ, 0.666: À§, 1: ¾Æ·¡ void Start() { animator = GetComponentInChildren(); playerAttack = GetComponent(); // Ű À̺¥Æ®¿Í ½ºÇÁ¶óÀÌÆ® ¹× ȸÀü ó¸® ¿¬°á 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 (playerState == PlayerState.Dash || playerState == PlayerState.Attack) 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; // À̵¿ ¿©ºÎ ÆÇº° if (moveDirection != Vector2.zero) { animator.SetBool("isWalking", true); playerState = PlayerState.Walking; 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(anim_Strings[0], 1); animator.SetFloat(anim_Strings[1], 0); } else { animator.SetFloat(anim_Strings[0], 0); animator.SetFloat(anim_Strings[1], moveDirection.y); } // ȸÀü ó¸® if (Mathf.Abs(moveDirection.x) > 0) { PlayerRot(moveDirection); } } else { // À̵¿ÇÏÁö ¾ÊÀ¸¸é Idle »óŸ¦ ¼³Á¤ moveTime = 0f; animator.SetBool("isWalking", false); animator.SetFloat(anim_Strings[0], 0); animator.SetFloat(anim_Strings[1], 0); animator.SetFloat(anim_Strings[2], idleState); } } private void PlayerRot(Vector2 direction) { if (direction.x < 0 && transform.GetChild(0).localEulerAngles.y != 180) { transform.GetChild(0).localEulerAngles = new Vector3(0, 180, 0); } else if (direction.x > 0 && transform.GetChild(0).localEulerAngles.y != 0) { transform.GetChild(0).localEulerAngles = new Vector3(0, 0, 0); } } float HandleAcceleration() { if (playerState == PlayerState.Walking) { moveTime += Time.deltaTime; if (moveTime >= accelerationTime) { return maxSpeed; // 2ÃÊ ÀÌ»ó À̵¿ ½Ã °¡¼Ó } } return moveSpeed; // ±âº» ¼Óµµ } // ´ë½Ã ó¸® void Dash() { if (playerState != PlayerState.Dash) { playerState = PlayerState.Dash; animator.SetTrigger("DodgeTrigger"); playerAttack.AttackStop(); moveTime = 0f; 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; playerState = PlayerState.Idle; } // À̵¿ ¹æÇâ°ú ½ºÇÁ¶óÀÌÆ®, ȸÀü ó¸® void SetMovement(Vector2 direction, float idleStateValue) { moveDirection = direction; idleState = idleStateValue; } }