Physics.RayCasting() 无法从GameObject内部检测撞击 Collider

        sphere1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere1.name = "Sphere1";
        sphere1.transform.position = Camera.main.transform.position + new Vector3(0, 0, 0.1f); // the camera is inside sphere1
        sphere1.transform.localScale = new Vector3(10, 10, 10); 
        sphere2= GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere2.name = "Sphere2";
        sphere2.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 3f;   // the camera is outside sphere1
        sphere2.transform.localScale = new Vector3(3,3,3); // Scale as needed

下面的常规碰撞检测代码,只能检测到sphere 2,而不能检测到sphere 1,尽管两个sphere都有sphere collider。 这是应为,camera在sphere 1内部,会直接输出"No hit"。 (Debug.DrawRay只会出现在scene view,而不会出现在gameview)

                GameObject controller = rightController;
                Ray ray = new Ray(controller.transform.position, controller.transform.forward);
                RaycastHit hit;

                if (Physics.Raycast(ray, out hit, 1000))
                {
                    // Here, you could check the name of the hit object or compare the GameObject directly
                    if (hit.collider.gameObject == sphere1.gameObject)
                    {
					// logic
                }
                else
                {
                    Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 10f);
                    Debug.Log(ray.origin);
                    Debug.Log(ray.direction);
                    Debug.Log("No hit");
                }

在我的程序中,我使用sphere 1来展示360图片,因此我的相机必须在sphere内部。一种解决方案是,从sphere1外面射出raycast,如下代码所示(还未测试):

public void OnOptionSelected(InputAction.CallbackContext context) {
    if (context.action.name == "SelectOption") {
        if (context.performed) {
            GameObject controller = rightController;
            
            // Assuming sphereRadius is the radius of your sphere
            float sphereRadius = 10.0f; // Replace with your actual sphere radius
            
            // Start the ray just outside the sphere's surface
            Vector3 rayOriginOutsideSphere = controller.transform.position - controller.transform.forward * sphereRadius;
            Ray ray = new Ray(rayOriginOutsideSphere, controller.transform.forward);
            RaycastHit hit;

            // Increase the maxDistance by the sphereRadius to account for the new origin
            float maxDistance = 1000 + sphereRadius;

            if (Physics.Raycast(ray, out hit, maxDistance)) {
                // Here, you could check the name of the hit object or compare the GameObject directly
                if (hit.collider.gameObject == sphereDisplay.gameObject) {
                    // This is where you handle the hit
                    OnClickToSelectOption("SphereDisplay_" + hit.collider.gameObject.name, "Sequential");
                    preferedOptionAreaID = curDisplayedOptionAreaID;
                }
            } else {
                Debug.DrawRay(ray.origin, ray.direction * maxDistance, Color.red, 10f);
                Debug.Log(ray.origin);
                Debug.Log(ray.direction);
                Debug.Log("No hit");
            }
        }
    }
}

在这里插入图片描述
(在上图中,360图片的球体是不能使用从camera射出的raycast进行检测的。但是白色的小球和正方体可以。)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值