코루틴을 사용해 플레이어가 탄환에 맞으면 깜빡과
목숨이 2개 이하면 화면에 붉은 깜빡이 효과를 넣음.
- ui를 덮는 반투명 붉은 이미지를 넣어 조건이 달성되면 0.3초간 나타났다 사라졌다를 무한 반복시킴
게임오브젝트가 아니라 TextMeshProUGUI로 텍스트를 불러와 목숨 2개 이하면 색 변경.
플레이어를 프리팹화하여 게임 씬을 재로딩 하고, 점수를 저장...
- Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
float moveSpeed = 5f;
int maxHp = 5;
float delta = 0;
Renderer renderer;
Color rendererColor;
public UICtrl uICtrl;
private void Start()
{
UICtrl uICtrl = GetComponent<UICtrl>();
renderer = this.gameObject.GetComponent<Renderer>();
rendererColor = renderer.material.color; // 기본 칼러 저장
FindObjectOfType<Bullet>();
Bullet bullet = GetComponent<Bullet>();
}
void Update()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
// Debug.Log(moveSpeed);
Vector3 dir = new Vector3(h, 0, v); //방향
Vector3 movement = dir.normalized * this.moveSpeed * Time.deltaTime;
this.transform.Translate(movement);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Finish")
{
// Debug.Log("총 맞은 것처럼♪");
Destroy(other.gameObject);
}
uICtrl.Life();
}
public void DeathColor()
{
// Debug.Log("오냐?");
renderer = this.gameObject.GetComponent<Renderer>();
if (renderer != null)
{
// Debug.Log(renderer.gameObject.name); // 어떤 객체의 랜더러를 불러오는지 이름 출력
renderer.material.color = Color.red;
}
}
public IEnumerator HitColor() // 무한이 아닌 한 번만 실행
{
renderer.material.color = Color.red;
yield return new WaitForSeconds(0.1f);
renderer.material.color = rendererColor;
}
}
- Ground
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ground : MonoBehaviour
{
void Update()
{
transform.Rotate(0, 0.03f, 0);
}
}
- Pillar
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pillar : MonoBehaviour
{
Vector3 pPiont;
Player player;
void Start()
{
player = FindObjectOfType<Player>();
}
// Update is called once per frame
void Update()
{
pPiont = player.transform.position;
transform.LookAt(pPiont);
}
}
- Bullet
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
public class Bullet : MonoBehaviour
{
float speed = 1.1f;
Rigidbody rb;
void Update()
{
rb = GetComponent<Rigidbody>();
rb.velocity = transform.forward * speed;
}
}
- BulletGenerator
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class BulletGenerator : MonoBehaviour
{
Player player;
Vector3 pPiont;
float delta = 0;
public GameObject BulletFrepabs;
void Start()
{
player = FindObjectOfType<Player>();
}
void Update()
{
delta += Time.deltaTime;
// Debug.Log(delta);
if (delta > 2.5)
{
delta = 0;
Instantiate(BulletFrepabs, transform.position, transform.rotation);
BulletFrepabs.transform.LookAt(player.transform);
}
}
}
- UICtrl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.VisualScripting;
using UnityEngine.SceneManagement;
public class UICtrl : MonoBehaviour
{
public TextMeshProUGUI TimeCount;
public TextMeshProUGUI LifeCount;
int maxHp = 5;
float delta = 0;
public Player player;
public GameObject redFilter;
void Start()
{
player = FindObjectOfType<Player>();
}
void Update()
{
delta += Time.deltaTime;
this.TimeCount.GetComponent<TMP_Text>().text = delta.ToString("Time: 0");
this.LifeCount.GetComponent<TMP_Text>().text = maxHp.ToString($"Life: {maxHp}");
}
public void Life()
{
// Debug.Log("악");
this.maxHp -= 1;
// Debug.Log($"현재 체력: {maxHp}");
player.StartCoroutine(player.HitColor()); //다른 클래스에 포함된 코루틴 실행
if (this.maxHp <= 2)
{
LifeCount.color = Color.yellow;
StartCoroutine(RedFilter());
}
if (maxHp <= 0)
{
// Debug.Log("주금");
player.DeathColor();
Time.timeScale = 0f;
//if (Input.GetMouseButtonDown(0))
//{
// SceneManager.LoadScene("SampleScene");
//}
}
}
IEnumerator RedFilter()
{
while (true) // 무한 반복
{
redFilter.gameObject.SetActive(true);
yield return new WaitForSeconds(0.3f);
redFilter.gameObject.SetActive(false);
yield return new WaitForSeconds(0.3f);
}
}
}