Unity 내배캠 TIL

Unity 게임 개발 최종 팀 프로젝트(11주차 2일)

오늘도즐겨 2024. 12. 3. 22:25

 

private void Update()
    {
        // 마우스 왼쪽 버튼 클릭 감지
        if (Input.GetMouseButtonDown(0)) // 1 : 오른쪽 버튼
        {
            // 마우스 위치에서 Ray를 생성
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            // Ray가 오브젝트에 충돌했는지 확인
            if (Physics.Raycast(ray, out hit))
            {
                // 충돌된 오브젝트의 GameObject 가져오기
                selectedUnit = hit.collider.gameObject;

                if (selectedUnit.TryGetComponent(out Unit unit) && unit is PlayerUnit) //(selectedUnit.CompareTag(nameof(Unit)))
                {
                    // dummyUnit = selectedUnit.GetComponent<DummyUnit>();
                    playerUnit = unit;
                    SetUnitInfo();
                    // 디버그 메시지 출력
                    Debug.Log("Selected Unit: " + unit.name);
                }
            }
        }
    }

 

 

selectedUnit 을 체크하고, 선택된 유닛의 태그가 Unit이라면,

그것을 DummyUnit이라고 하고, DummyUnitScript를 붙여줘라.

selectedUnit = hit.collider.gameObject;
if(selectedUnit.CompareTag(nameof(Unit)))
	dummyUnit = selectedUnit.GetComponent<DummyUnit>();

 

 

selectedUnit 을 체크하고, 선택된 유닛이 가지고 있는 스크립트를 뱉어주는 로직

selectedUnit = hit.collider.gameObject;

if (selectedUnit.TryGetComponent(out Unit unit)&& unit is PlayerUnit)
	playerUnit = unit;

 

 

 

📌오디오소스를 필수적으로 넣어주는 명령어

using UnityEngine;

[RequireComponent(typeof(AudioSource))]  // AudioSource 컴포넌트를 필수로 요구
public class PlaySound : MonoBehaviour
{
    private AudioSource audioSource;

    void Start()
    {
        // AudioSource 컴포넌트를 가져옴
        audioSource = GetComponent<AudioSource>();

        // 오디오 재생
        audioSource.Play();
    }
}