유니티 기초
선택된 바구니 게임씬에서 가져오기
노재두내
2023. 8. 7. 16:56
audio 부분에서 오류남--> 바구니 프리팹에서 audio source 컴포넌트 추가 안한거였음
basketController 스크립트의 GameObject.Find("GameDirector") 에서 null refernce오류남 --> lobbyScene에서의 바구니에 basketController스크립트가 있었는데 lobbyScene에서의 바구니에는 스크립트가 필요없고 , lobbyScene에는 GameDirector 게임 오브젝트가 없으니까 오류났던것
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasketController : MonoBehaviour
{
private AudioSource audioSource;
[SerializeField]
private AudioClip appleSfx;
[SerializeField]
private AudioClip bombSfx;
[SerializeField]
private GameDirector gameDirector;
// Start is called before the first frame update
private void Awake()
{
Debug.Log("<color=cyan>Awake</color>");
GameObject gameDirectorGo = GameObject.Find("GameDirector");
Debug.LogFormat("Awake:{0}", gameDirectorGo);
this.gameDirector = gameDirectorGo.GetComponent<GameDirector>();
Debug.LogFormat("gameDirector:{0}", gameDirector);
}
void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
//마우스 왼쪽 클릭하면 화면을 클릭하면
//ray를 만들자
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float maxDistance = 10f;
Debug.DrawRay(ray.origin, ray.direction* maxDistance, Color.green, 1f);
//out매개변수를 사용하려면 변수 정의르르 먼저 해야한다.
RaycastHit hit;
//out키워드를 사용해ㅓㅅ 인자로 넣어라
//Raycast 메서드에서 연산된 결과를 hit에 넣어줌
if(Physics.Raycast(ray, out hit, maxDistance))
{
Debug.Log(hit.point);
//바구니 위치를 충돌한 지점으로 이동
// this.gameObject.transform.position = hit.point;
//x,z 좌표의 반올림
float x = Mathf.RoundToInt(hit.point.x);
float z = Mathf.RoundToInt(hit.point.z);
//새로운 좌표를 만든다
this.transform.position=new Vector3(x, 0, z);
}
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.tag);
if (other.tag == "apple")
{
Debug.Log("점수추가");
this.audioSource.PlayOneShot(this.appleSfx);
this.gameDirector.IncreaseScore(100);
Destroy(other.gameObject);
}
else if(other.tag=="bomb")
{
Debug.Log("감점");
this.audioSource.PlayOneShot(this.bombSfx);
this.gameDirector.DecreaseScore(50);
Destroy(other.gameObject);
}
this.gameDirector.UpdateScoreUI();
}
}
분홍색은 잘되는데 민트와 , 보라색은 위치가 이상함
-->
lcosphere의 위치를 건들였는지 0,0,0 이 아니었음 (0,0,0)으로 바꾸니 위치가 잘 잡힘