└▶연습
과일 바구니1
101won
2024. 9. 17. 19:17
레이를 사용해 마우스 위치로 바구니 이동
1초 간격으로 사과 생성
바구니에 닿으면 사과 삭제
폭탄과 사과 삭제 시, 다른 사운드 출력
- 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;
Debug.Log(delta);
if(dropTime < delta)
{
delta = 0;
Instantiate(appGo);
}
}
}