【发布时间】:2020-05-23 09:01:09
【问题描述】:
我想从 Sphere GameObject 的中心进行射线投射,以找到被射线击中的文本/图像元素。我正在使用 Physics.RaycastAll 来检测被击中的 UI 元素。以下是我的代码。
public class RaycastUI : MonoBehaviour
{
public GameObject Sphere;
private Vector3 _origin;
private Vector3 _direction;
void Update()
{
RaycastHit[] hits;
// get the origin and direction
_origin = Sphere.transform.position;
_direction = Sphere.transform.forward;
//raycast all
hits = Physics.RaycastAll(_origin, _direction, 100.0F);
// retrieve the names of the element that are hit by the sphere.
for (int i = 0; i < hits.Length; i++) {
RaycastHit hit = hits[i];
Debug.Log(hit.collider.name);
}
}
}
我已将 BoxCoilloider 添加到画布中。不幸的是,hits.Length 总是返回 0。我错过了什么吗?如何从球体投射到画布中的 ui 元素以检查它是否发生碰撞?
【问题讨论】:
-
您可能需要将它放在可以用作光线投射目标的透明图像上
-
你的意思是我应该把我的按钮放在透明图像中吗?
-
不,按钮可以有图像,但是纯文本没有。
标签: c# unity3d user-interface virtual-reality raycasting