using TMPro; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //코드로 ui를 건들기 위해서 임포트 public class PlayerController : MonoBehaviour { private Rigidbody playerRigidbody; public float speed = 8f; public int shieldCount = 3; //초기 실드 수 private bool isShieldActive = false; //실드 발동여부 public GameObject shieldVisual; //하이라이어키 실드 public Image shieldIcon; // 실드 이미지 아이콘 public TextMeshProUGUI countText; //글자 public void Die() { gameObject.SetActive(false); GameManager gm = FindFirstObjectByType(); gm.EndGame(); } // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { playerRigidbody = GetComponent(); UpdateShieldUI(); //함수발동 } // Update is called once per frame void Update() { float x = Input.GetAxis("Horizontal"); float z = Input.GetAxis("Vertical"); float xSpeed = x * speed; float zSpeed = z * speed; bool moveInput = (x != 0 || z != 0); Vector3 newVelocity = new Vector3(xSpeed, 0f, zSpeed); playerRigidbody.linearVelocity = newVelocity; if(moveInput) { Vector3 moveDirection = new Vector3(x, 0f, z); Quaternion newRotation = Quaternion.LookRotation(moveDirection); transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 0.2f); } if (Input.GetKeyDown(KeyCode.Alpha1) && shieldCount > 0 && !isShieldActive) //1숫자가 눌리고 카운트가 1이상이고 실드가 활동한 상태이면 { StartCoroutine(ActivateShield()); //비동기적으로 함수를 발동하라 } Animator anim = GetComponentInChildren(); if (anim != null) { anim.SetBool("isMoving", moveInput); } } void UpdateShieldUI() { if (countText != null) //텍스트가 비어있지 않다면 { countText.text = shieldCount.ToString(); //실드의 카운트를 문자열로 저장 if (shieldCount <= 0) { countText.gameObject.SetActive(false); //0이하면 활동에 페일즈를 저장 if (shieldIcon != null) { shieldIcon.color = new Color(0.2f, 0.2f, 0.2f, 0.5f); } } } } IEnumerator ActivateShield() //코루틴 { isShieldActive = true; //실드는 트루로 변경 shieldCount--; //실드 카운트는 감소 UpdateShieldUI(); //업데이트 if (shieldVisual != null) shieldVisual.SetActive(true); //만약에 실드비주얼이 비어있지 않다면 트루를 저장 yield return new WaitForSeconds(2f); // 2초동안만 if (shieldVisual != null) shieldVisual.SetActive(false); //만약에 실드비주얼이 비어있지 않다면면 페일즈 isShieldActive = false; //실드 페일즈 } }