└▶연습

탄막3

101won 2024. 10. 6. 00:19

 

밑 판을 돌리고 ui 컨트롤을 제작해 플레이어에 있던 목숨 카운트 매서드를 옮겼다.

옮기는 과정에서 죽었을 때 컬러가 변하지 않는 문제가 있었는데 매서드 시작이 아닌 스타트할 때 플레이어를 찾게해서 수정했다. 게임 상에 있는 오브젝트면 미리 찾아 놓자.

시간과 라이프 카운트를 함.

 

플레이어가 총알에 닿을 때마다 색이 바뀌게 하고 싶다. 코루틴을 써야겠지.

목숨이 2개 이하면 폰트 색 변경도 할 수 있을 거 같다.

게임 스타트와 재시작도 복습해야지.

 

  • Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    float moveSpeed = 5f;
    int maxHp = 5;
    float delta = 0;

    public UICtrl uICtrl;

    private void Start()
    {

        FindObjectOfType<Bullet>();
        Bullet bullet = GetComponent<Bullet>();

        UICtrl uICtrl = GetComponent<UICtrl>();
    }

    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 renderer = this.gameObject.GetComponent<Renderer>();
        if (renderer != null)
        {
           // Debug.Log(renderer.gameObject.name);
            renderer.material.color = Color.red;
        }
    }
}

 

 

  • 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;

public class UICtrl : MonoBehaviour
{
    public GameObject TimeCount;
    public GameObject LifeCount;
 
    int maxHp = 5;
    float delta = 0;

    public Player player;

    void Start()
    {
        Player player = GetComponent<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}");

        if (maxHp <= 0)
        {
            // Debug.Log("주금");

            player.DeathColor();

            Time.timeScale = 0f;
        }
    }
}

 

이번 건 매서드를 좀 깔끔하게 나누어 사용한 것 같아서 뿌듯.