问题
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;
This is the basic Raycast script I have .
回答1:
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.
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)
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);
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
来源:https://stackoverflow.com/questions/37252090/c-sharp-unity-raycast-through-an-object-without-having-to-ignore-it-and-still-hi