30초 10초 이하 일 때 드랍 속도 변경

시간 오버 시,  탭 키 눌러서 게임 신 재로드

오늘 배운 Time.timeScale = 0f;으로 속도 변경했는데 드랍이 안나왔음 > 드랍제너레이터 문제인 줄 알고 로그 찍어봤는데 아예 안찍힘

그래서 스타트 매서드를 추가하고 Time.timeScale = 1f; 로 바꿨더니 됨. 이제 게임 오버 할 수 있다.

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

public class basket : MonoBehaviour
{
    AudioSource audioSource; // 오디오 컴퍼넌트: 1개만 등록 가능
    
    public AudioClip bombS; // 사운드 클립 : 2개 이상 쓸 때 직접 등록
    public AudioClip appS;

    public UICtr UICtr;
    
    void Update()
    {
        // 스크린 좌표로 마우스를 받은 후, 월드 좌표로 변환해 레이를 쏜ㄷㅏ.
        if (Input.GetMouseButtonDown(0))
        {
            // 마우스에 입력된 스크린 좌표를 월드 좌표 레이에 할당
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);  
         
            RaycastHit hit; // 콜라이더 충돌 감지 광선
           

           if(Physics.Raycast(ray, out hit, Mathf.Infinity)) // 레이 충돌 지점 찾 거리 무한대
            {
                float x = Mathf.RoundToInt(hit.point.x); // 소수점 이하 생략, 정수로 반환
                float z =Mathf.RoundToInt(hit.point.z);

                transform.position = new Vector3(x, 0, z);

               // Debug.Log(new Vector3(x, 0, z));
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        audioSource = GetComponent<AudioSource>();

        if (other.gameObject.tag == "apple")
        { 
           // Debug.Log("사과");

            audioSource.PlayOneShot(this.appS); // 플레이 할 사운드가 여러 개 일 때 사용
            UICtr.ScoreApple();

        }

        if (other.gameObject.tag == "bomb")
        { 
            // Debug.Log("폭탄");

            audioSource.PlayOneShot(this.bombS);
            UICtr.ScoreBomb();
        }
        Destroy(other.gameObject); // 아더로 들어온 오브젝트를 삭제
        // Debug.Log("으앙 주금");
    }
}

 

 

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

public class drop : MonoBehaviour
{

    void Update()
    {
        transform.Translate(0, -0.003f, 0);

        if(transform.position.y < -1)
        {
            Destroy(gameObject); // 자신을 없앰
        }
    }
}

 

  • DropGenerator
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class DropGenerator : MonoBehaviour
{
    public GameObject appGo;
    public GameObject bombGo;
    
    public float dropTime = 1f; // ui에서 스크립트를 가져가 시간 변경할 수 있게 함
    float delta = 0; // 업데이트 밖에서 선언해야 함

    void Update()
    {
        delta += Time.deltaTime;
       
        if(dropTime < delta)
        {
         Debug.Log($"<color=yellow>드드드드드랍랍랍랍랍</color>");
         
            delta = 0;

            int dropResult = Random.Range(1, 3); // 1이면 사과, 2면 폭탄 프리팹 생성

            if (dropResult == 1)
            {
                Instantiate(appGo);

                float randX = Random.Range(-1, 2);
                float randz = Random.Range(-1, 2);

                appGo.transform.position = new Vector3(randX, 3, randz);
            }
            else
            {
                Instantiate(bombGo);

                float randX = Random.Range(-1, 2);
                float randz = Random.Range(-1, 2);

                bombGo.transform.position = new Vector3(randX, 3, randz);
            }
        }
    }
}

 

  • UICtr
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro; // 겟컴포넌트로 쓸 거니까 있어야댕
using UnityEngine.SceneManagement; // 씬 변경

public class UICtr : MonoBehaviour
{

   public GameObject GameTime; // 불러올 때는 겜오브젝트
   public GameObject Score;
   public GameObject GOText;

   public GameObject dropGenerator;
   
    public float delta = 5; // 이게 업데이트 안에 있으면 계속 시작함 ㅠㅠ
    int getScore = 0; // 선언 및 할당

    private void Start()
    {
        this.ChangeDropTime();
        Time.timeScale = 1f; // 시작 시, 원래 플레이 속도
    }

    void Update()
    {
        delta -= Time.deltaTime;
        //   Debug.Log(delta);

        this.GameTime.GetComponent<TMP_Text>().text = delta.ToString("F2"); // 사용은 겟컴포넌트

        this.Score.GetComponent<TMP_Text>().text = getScore.ToString();
     

        if (delta <= 0)
        {
           GOText.SetActive(true); 

            GameOver();
          //  Debug.Log($"<color=yellow>GameOver</color>");
        }
    }

    public void ScoreApple()
    {
        this.getScore += 10;
    }  
    
    public void ScoreBomb()
    {
        this.getScore /= 2;
    }

    void ChangeDropTime()
    {
        if (delta < 30)
        {
            this.dropGenerator.GetComponent<DropGenerator>().dropTime = 0.5f; // 스크립트를 겟컴포넌트
           //  Debug.Log("30초");
        }
        if (delta < 10)
        {
            this.dropGenerator.GetComponent<DropGenerator>().dropTime = 0.2f;
           //  Debug.Log("10초");
        }
    }

    void GameOver()
    {
      Time.timeScale = 0f;

        if (Input.GetKeyDown(KeyCode.Tab))
        {
            SceneManager.LoadScene("basket"); // 게임 씬을 새로 불러와 재시작
        }
    }
}

 

'└▶연습' 카테고리의 다른 글

룰렛3  (0) 2024.09.21
룰렛2  (0) 2024.09.21
과일바구니2  (0) 2024.09.20
과일 바구니1  (1) 2024.09.17
밤송이2: 스크린 좌표 -> 월드 좌표  (0) 2024.09.17

+ Recent posts