问题描述
Esstinaly 我想做的是让 Raycast 通过一个物体并在不忽略第一个物体的情况下击中另一个物体,所以如果玩家将鼠标放在第一个物体上而不是第二个物体上,它只会击中第一个物体对象,但如果鼠标悬停在两个对象上,它只会击中第二个对象,如果可能,我可以提供视觉参考,只是显示有关所述第二个对象的一些文本.
Esstinaly what I am trying to do is have a Raycast pass though an object and hit another object inside without ignoring the first object, so if the player has there mouse over the first object but not the second it will only hit the first object but if the mouse is over both it will hit only the second object, I can provide visual reference if possible it is only to display some text about said second object.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
{
NameTag = hitInfo.transform.name; //Send information of the Object to the GUI Label
hoverOverActive = true;
这是我拥有的基本 Raycast 脚本.
This is the basic Raycast script I have .
推荐答案
您正在寻找的是 Raycast Mask.这允许您指定一个层,当 Raycast 击中它时,它将被忽略.
What you are looking for is a Raycast Mask. This allows you to specify a layer of which when a Raycast hits it, it will be ignored.
在 Unity Editor 中,您需要创建一个新层并记住它的值,例如 ignoreLayer
In the Unity Editor you will need to create a new layer and remember its value,e.g ignoreLayer
在您的光线投射代码中,您需要将此图层设置为您的遮罩"以使其被忽略:例如
In your raycast code you will need to set this layer as your 'mask' in order for it to become ignored:e.g
public int ignoreLayer = 8
In, Physics.Raycast(ray, out hitInfo)
您在此处指定您的掩码,因为您可以选择将其传入 Physics.Raycast
Unity 文档链接:http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
In, Physics.Raycast(ray, out hitInfo)
you specify your mask here as you can pass it in optionally to Physics.Raycast
Link to Unity Documentation: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
public static bool Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
Youtube 上的这个视频非常非常好地解释了如何使用 Raycast Masking,所以我也会为您链接:https://www.youtube.com/watch?v=XO6KCCtGPpA
This video on Youtube explains how to use Raycast Masking very, very well so I'll link this in for you also: https://www.youtube.com/watch?v=XO6KCCtGPpA
这篇关于C# Unity Raycast 通过一个对象而不必忽略它并仍然击中它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!