플레이어에 체력 5를 부여해 탄을 5개 맞으면 색이 변경되어 죽었다는 걸 알리고 게임을 정지함
material은 랜더러에 속해 있어 랜더러를 불러와 사용할 수 있었다.
바닥을 돌리고, ui에 반영해야지.
- Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
float moveSpeed = 5f;
int maxHp = 5;
private void Start()
{
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);
}
Life();
}
void Life()
{
// Debug.Log("악");
maxHp -= 1;
// Debug.Log($"현재 체력: {maxHp}");
if (maxHp <= 0)
{
// Debug.Log("주금");
Renderer renderer = this.gameObject.GetComponent<Renderer>();
renderer.material.color = Color.red;
Time.timeScale = 0f;
}
}
}