Physics.Raycast 버그 잡음

This commit is contained in:
logonkhi
2025-06-24 14:38:58 +09:00
parent fa773ad3a3
commit 3acff06eca
4458 changed files with 3510 additions and 576 deletions

View File

@@ -0,0 +1,42 @@
using UnityEngine;
public class RaycastDebugger : MonoBehaviour
{
private Camera mainCamera;
void Start()
{
// Camera.main 대신 시작할 때 한 번만 카메라를 찾아오는 것이 더 안전하고 효율적입니다.
mainCamera = Camera.main;
if (mainCamera == null)
{
Debug.LogError("씬에 'MainCamera' 태그가 붙은 카메라가 없습니다!");
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (mainCamera == null) return;
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Scene 뷰에서 Ray를 시각적으로 확인하기 위한 디버그용 코드
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.yellow, 120f);
// Ray 객체를 직접 넘기는 것이 더 간단합니다.
// 거리는 무한대(Mathf.Infinity)로 설정합니다.
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Raycast Hit! 맞은 객체: " + hit.collider.name);
// 여기에 원하는 로직을 추가하세요.
}
else
{
Debug.Log("Raycast Missed. 아무것도 맞지 않았습니다.");
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9958aa39fc037e74090efe980e62c959

View File

@@ -7,6 +7,9 @@ namespace UVC.Factory.Component
public class ClickTest : MonoBehaviour
{
private InputSystemActions inputSystemActions;
private Camera mainCamera;
//void Start()
//{
// Collider col = GetComponent<Collider>();
@@ -27,68 +30,84 @@ namespace UVC.Factory.Component
// }
//}
//void Update()
//{
// if (Input.GetMouseButtonDown(0)) // 0 represents the left mouse button
// {
void OnMouseDown()
{
Debug.Log($"OnMouseDown: {gameObject.name}");
transform.localScale -= new Vector3(0.05f, 0.05f, 0);
//or
//transform.GetComponent<SpriteRenderer>().color += new Color(40, 40, 40);
}
// Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// RaycastHit hit;
void OnMouseUp()
{
Debug.Log($"OnMouseUp: {gameObject.name}");
transform.localScale += new Vector3(0.05f, 0.05f, 0);
//or
//transform.GetComponent<SpriteRenderer>().color -= new Color(40, 40, 40);
}
// // 레이 시작점과 방향 로깅
// Debug.Log($"Ray 원점: {ray.origin}, 방향: {ray.direction}");
void Update()
{
if (UnityEngine.Input.GetMouseButtonDown(0)) // 0 represents the left mouse button
{
// // 디버그용 레이를 씬 뷰에 그립니다
// Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 2f);
Ray ray = Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);
RaycastHit hit;
// bool hitSomething = Physics.Raycast(ray, out hit);
// Debug.Log($"Mouse button clicked - hit something: {hitSomething}");
// 레이 시작점과 방향 로깅
Debug.Log($"Ray 원점: {ray.origin}, 방향: {ray.direction}");
// if (hitSomething)
// {
// Debug.Log($"Hit object: {hit.collider.gameObject.name}, distance: {hit.distance}, point: {hit.point}");
// }
// else
// {
// // 모든 콜라이더 로깅
// Collider[] allColliders = FindObjectsOfType<Collider>();
// Debug.Log($"씬에 있는 콜라이더 수: {allColliders.Length}");
// 디버그용 레이를 씬 뷰에 그립니다
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 60f);
// foreach (var collider in allColliders)
// {
// if (collider.gameObject.activeInHierarchy && collider.enabled)
// Debug.Log($"활성화된 콜라이더: {collider.gameObject.name}, 레이어: {LayerMask.LayerToName(collider.gameObject.layer)}");
// }
// }
bool hitSomething = Physics.Raycast(ray, out hit);
Debug.Log($"Mouse button clicked - hit something: {hitSomething}");
// ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// RaycastHit[] tempinfo = new RaycastHit[100];
// var hitcount = Physics.RaycastNonAlloc(ray, tempinfo, Mathf.Infinity);
// Debug.Log($"Mouse button clicked hitcount:{hitcount} {Physics.Raycast(ray, out hit)}");
if (hitSomething)
{
Debug.Log($"Hit object: {hit.collider.gameObject.name}, distance: {hit.distance}, point: {hit.point}");
}
else
{
// 모든 콜라이더 로깅
Collider[] allColliders = FindObjectsByType<Collider>(FindObjectsSortMode.None);
Debug.Log($"씬에 있는 콜라이더 수: {allColliders.Length}");
// if (Physics.Raycast(ray, out hit))
// {
// // Check if the hit object has the ClickableObject script
// ClickTest clickableObject = hit.collider.GetComponent<ClickTest>();
// if (clickableObject != null)
// {
// // 수동으로 이벤트 호출
// PointerEventData pointerData = new PointerEventData(EventSystem.current);
// pointerData.position = Input.mousePosition;
// OnPointerClick(pointerData);
// }
// else
// {
// Debug.Log("Clicked on: " + hit.collider.gameObject.name + ", but it's not clickable.");
// }
// }
// }
foreach (var collider in allColliders)
{
if (collider.gameObject.activeInHierarchy && collider.enabled)
Debug.Log($"활성화된 콜라이더: {collider.gameObject.name}, 레이어: {LayerMask.LayerToName(collider.gameObject.layer)}");
}
}
// //if (Mouse.current.leftButton.wasPressedThisFrame)
// //{
// // Debug.Log("마우스 클릭 감지");
// //}
//}
ray = Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);
RaycastHit[] tempinfo = new RaycastHit[100];
var hitcount = Physics.RaycastNonAlloc(ray, tempinfo, Mathf.Infinity);
Debug.Log($"Mouse button clicked hitcount:{hitcount} {Physics.Raycast(ray, out hit)}");
if (Physics.Raycast(ray, out hit))
{
// Check if the hit object has the ClickableObject script
ClickTest clickableObject = hit.collider.GetComponent<ClickTest>();
if (clickableObject != null)
{
// 수동으로 이벤트 호출
PointerEventData pointerData = new PointerEventData(EventSystem.current);
pointerData.position = UnityEngine.Input.mousePosition;
OnPointerClick(pointerData);
}
else
{
Debug.Log("Clicked on: " + hit.collider.gameObject.name + ", but it's not clickable.");
}
}
}
//if (Mouse.current.leftButton.wasPressedThisFrame)
//{
// Debug.Log("마우스 클릭 감지");
//}
}
public void OnPointerClick(PointerEventData eventData)
{

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 745da5f00db92f84cb12049e0ca9cd85
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,58 @@
using UnityEngine;
namespace UVC.input
{
public class InputHandler : MonoBehaviour
{
public Transform clickedObject;
// 프레임당 한 번씩 업데이트가 호출됩니다
void Update()
{
// 터치 입력 확인
if (UnityEngine.Input.touchCount > 0)
{
if (UnityEngine.Input.GetTouch(0).phase == TouchPhase.Began)
{
handleClick(UnityEngine.Input.GetTouch(0).position);
}
}
// 왼쪽 마우스 버튼 클릭 확인
if (UnityEngine.Input.GetMouseButtonDown(0))
{
handleClick(UnityEngine.Input.mousePosition);
}
}
void handleClick(Vector3 screenClickPosition)
{
// 클릭한 위치에서 카메라에 수직인 Ray를 그립니다
// Ray가 교차하는 첫 번째 객체를 캡처하기 위해 RayHit를 생성합니다
Ray ray = Camera.main.ScreenPointToRay(screenClickPosition);
RaycastHit rayHit;
if (Physics.Raycast(ray.origin, ray.direction, out rayHit))
{
Debug.Log($"Ray Origin: {ray.origin}, Direction: {ray.direction}");
if (rayHit.transform.tag == "Clickable")
{
// 클릭한 객체에 어떤 작업을 합니다.
clickedObject = rayHit.transform;
// 다음 클릭을 위해 ray와 rayHit을 재설정합니다.
ray = new Ray();
rayHit = new RaycastHit();
}
else if (clickedObject != null)
{
// 클릭한 객체의 y 위치에서 xz 축에 평면을 그립니다.
Plane xzPlane = new Plane(Vector3.up, clickedObject.position);
float distance; // 클릭과 평면이 교차 하는 지점을 찾습니다.
// 그리고 클릭한 객체를 새로운 위치로 이동합니다.
if (xzPlane.Raycast(ray, out distance))
{
clickedObject.position = ray.GetPoint(distance);
}
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8de502d6dd9c6114f993b104854864c5

View File

@@ -0,0 +1,79 @@
using UnityEngine;
using UnityEngine.InputSystem;
namespace UVC.input
{
public class MouseInputHandler : MonoBehaviour
{
private MouseControls mouseControls;
private Camera mainCamera;
private GameObject lastHoveredObject = null;
private void Awake()
{
mainCamera = Camera.main;
mouseControls = new MouseControls();
}
private void OnEnable()
{
mouseControls.Player.Enable();
mouseControls.Player.Click.performed += OnClick;
}
private void OnDisable()
{
mouseControls.Player.Disable();
mouseControls.Player.Click.performed -= OnClick;
}
void Update()
{
// 마우스 위치로 Ray 발사
Ray ray = mainCamera.ScreenPointToRay(mouseControls.Player.Point.ReadValue<Vector2>());
RaycastHit hit;
GameObject currentHoveredObject = null;
if (Physics.Raycast(ray, out hit))
{
// Ray에 맞은 객체가 있을 경우
currentHoveredObject = hit.collider.gameObject;
}
// 마우스 오버/아웃 상태 변경 감지
if (currentHoveredObject != lastHoveredObject)
{
// 이전에 가리키던 객체가 있었다면 Mouse Out 이벤트 호출
if (lastHoveredObject != null)
{
// 여기에 Mouse Out 로직을 추가하세요.
Debug.Log("Mouse Out: " + lastHoveredObject.name);
// 예: lastHoveredObject.GetComponent<Renderer>().material.color = Color.white;
}
// 현재 가리키는 객체가 있다면 Mouse Over 이벤트 호출
if (currentHoveredObject != null)
{
// 여기에 Mouse Over 로직을 추가하세요.
Debug.Log("Mouse Over: " + currentHoveredObject.name);
// 예: currentHoveredObject.GetComponent<Renderer>().material.color = Color.red;
}
lastHoveredObject = currentHoveredObject;
}
}
private void OnClick(InputAction.CallbackContext context)
{
Debug.Log("Clicked");
// 현재 마우스가 가리키고 있는 객체가 있을 때만 클릭 이벤트 처리
if (lastHoveredObject != null)
{
// 여기에 Mouse Click 로직을 추가하세요.
Debug.Log("Clicked on: " + lastHoveredObject.name);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 72b9b338934540349a2d195547344dab