Files
WildRoot/Assets/Script/PlayerController.cs
T
jongjae0305 2e4370481b [Game] Dodge
1. 총알피하기 게임 제작
2. 시간별로 단계 설정
3. 시작화면 및 이펙트, 사운드 삽입
2026-04-14 17:06:58 +09:00

88 lines
2.6 KiB
C#

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<GameManager>();
gm.EndGame();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
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;
Vector3 newVelocity = new Vector3(xSpeed, 0f, zSpeed);
playerRigidbody.linearVelocity = newVelocity;
if (Input.GetKeyDown(KeyCode.Alpha1) && shieldCount > 0 && !isShieldActive) //1숫자가 눌리고 카운트가 1이상이고 실드가 활동한 상태이면
{
StartCoroutine(ActivateShield()); //비동기적으로 함수를 발동하라
}
}
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; //실드 페일즈
}
}