using System.Collections; using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5f; // À̵¿ ¼Óµµ public float dashDistance = 3f; // ´ë½Ã °Å¸® public float dashCooldown = 1f; // ´ë½Ã ÄðŸÀÓ 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(); animator = GetComponentInChildren(); // Ű À̺¥Æ®¿Í ½ºÇÁ¶óÀÌÆ® ¹× ȸÀü ó¸® ¿¬°á 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) { transform.position += (Vector3)(moveDirection.normalized * moveSpeed * Time.deltaTime); // ¾Ö´Ï¸ÞÀÌ¼Ç À̵¿ ¹æÇâ ¼³Á¤ animator.SetFloat("moveX", Mathf.Abs(moveDirection.x)); 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 »óŸ¦ ¼³Á¤ animator.SetFloat("moveX", 0); animator.SetFloat("moveY", 0); animator.SetFloat("idleState", idleState); } } // ´ë½Ã ó¸® 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; } }