과일과 폭탄을 랜덤하게 드랍

프리팹 일부가 남는 현상과 바구니가 y좌표 밖으로 나가는 현상이 있는데.. 버그 수정은 다 작동되게 한 후  나중에

 

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

public class basket : MonoBehaviour
{
    AudioSource audioSource; // 오디오 컴퍼넌트: 1개만 등록 가능
    
    public AudioClip bombS; // 사운드 클립 : 2개 이상 쓸 때 직접 등록
    public AudioClip appS;
    
    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); // 플레이 할 사운드가 여러 개 일 때 사용
        }

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

            audioSource.PlayOneShot(this.bombS);
        }
        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;
    
    float dropTime = 1f;
    float delta = 0; // 업데이트 밖에서 선언해야 함

   // 프리팹을 1초 간격으로 생성
    void Update()
    {
        delta += Time.deltaTime;
       
        if(dropTime < delta)
        {
            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);
            }
        }
    }
}

 

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

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

+ Recent posts