using System.Collections; using System.Collections.Generic; using TreeEditor; using UnityEngine; using UnityEngine.InputSystem; public class PlayerController : MonoBehaviour { [SerializeField] private Rigidbody2D rbody; private Vector2 moveInput; [SerializeField] private float speed = 3.0f; // 이동 속도 [SerializeField] private float dashspeed = 5.4f; [SerializeField] private float jumpForce = 5.0f; // 점프력 private float axisH = 0.0f; // 좌우 입력 값 저장 bool isDash = false; // 대시 플래그 bool goJump = false; // 점프 개시 플래그 bool isDead = false; // 사망 플래그 bool onGround = false; // 지면에 서 있는 플래그 public LayerMask groundLayer; // 착지할 수 있는 레이어 private Animator animator; private PlayerAttacks playerAttacks; /*[SerializeField] private GameObject nano; [SerializeField] private GameObject tera; bool isnano = false; bool istera = false;*/ void Start() { //Rigidbody2D 가져오기 rbody = GetComponentInChildren(); //Animator 가져오기 animator = GetComponentInChildren(); playerAttacks = GetComponent(); /*nano = GameObject.Find("player_nano"); tera = GameObject.Find("player_tera"); nano.SetActive(true); isnano = true; tera.SetActive(false);*/ } void OnMove(InputValue value) { // Vector2 값 중 x축(좌우) 값만 가져옴 Vector2 inputVector = value.Get(); axisH = inputVector.x; } void OnDash(InputValue value) { isDash = value.isPressed; if (isDash) Debug.Log("대시 중..."); else Debug.Log("대시 중단"); } public void OnJump(InputValue value) { // 버튼이 눌린 순간(isPressed)이고 지면 위라면 점프 예약 if (value.isPressed && onGround) { goJump = true; animator.SetBool("jump", true); Debug.Log(" 점프 버튼 눌림! "); } } public void OnSAtk(InputValue value) { Debug.Log(" s 버튼 눌림! "); if (value.isPressed && !playerAttacks.weaponInfos[0].isUse) { playerAttacks.weaponInfos[0].isUse = true; StartCoroutine(playerAttacks.ableAtk(0)); } } public void OnAAtk(InputValue value) { Debug.Log(" a 버튼 눌림! "); if (value.isPressed && !playerAttacks.weaponInfos[1].isUse) { playerAttacks.weaponInfos[1].isUse = true; StartCoroutine(playerAttacks.ableAtk(1)); } } /*public void OnTagKey(InputValue value) { if (!value.isPressed) return; Debug.Log(" Z 버튼 눌림! "); if (isnano) { tera.SetActive(true); nano.SetActive(false); isnano = false; istera = true; } else if (istera) { tera.SetActive(false); nano.SetActive(true); isnano = true; istera = false; } }*/ void Update() { if (isDead) return; bool isMoving = axisH != 0; // 지면 상태 확인 animator.SetBool("jump", !onGround); animator.SetBool("Dash", isDash); animator.SetBool("Move", isMoving); // 플래그 초기화 if (Keyboard.current.leftShiftKey.wasReleasedThisFrame) isDash = false; //if (Keyboard.current.sKey.wasReleasedThisFrame) issAtk = false; //if (Keyboard.current.aKey.wasReleasedThisFrame) isaAtk = false; // 방향 조절 if (isMoving) { float direction = axisH > 0 ? 1 : -1; transform.localScale = new Vector2(direction, 1); } else animator.SetBool("Dash", false); } private void FixedUpdate() { onGround = Physics2D.Linecast(transform.position, transform.position - (transform.up * 0.1f), groundLayer); if (isDead) return; // 공격중인지 확인 if (playerAttacks.IsAnyWeaponInUse) { rbody.linearVelocity = new Vector2(0, rbody.linearVelocity.y); // 정지 return; } float currentSpeed = (isDash && onGround) ? dashspeed : speed; rbody.linearVelocity = new Vector2(axisH * currentSpeed, rbody.linearVelocity.y); if (goJump) { Debug.Log("점프!"); rbody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); goJump = false; } } private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Goal")) { GameStop(); } else if (collision.CompareTag("Dead")) { GameOver(); } } public void GameStop() { rbody.linearVelocity = Vector2.zero; } public void GameOver() { isDead = true; animator.SetBool("Move", false); GameStop(); } }