xxxxxxxxxx
// Raycast Example 5: Raycast with Custom Layer Exclusion
// Description: This code performs a raycast from the object's position forward, excluding specific layers.
// Date: 2023-10-12
using UnityEngine;
public class Print : MonoBehaviour
{
public LayerMask customLayerMask; // Specify a custom layer mask.
public LayerMask excludedLayers; // Specify layers to be excluded.
void Update()
{
// Define a ray starting from the object's position and going forward.
Ray ray = new Ray(transform.position, transform.forward);
// Create a layer mask excluding specified layers.
int layerMask = customLayerMask & ~excludedLayers;
// Declare a variable to store information about the hit object.
RaycastHit hit;
// Check if the ray hits an object within the custom layer mask, excluding specified layers.
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
// Log the name of the hit object.
Debug.Log("Raycast hit within the custom layer mask (excluding specified layers): " + hit.transform.name);
}
}
}
xxxxxxxxxx
// Raycast Example 6: Raycast with Layer Exclusion
// Description: This code performs a raycast from the object's position forward, excluding specific layers, and ignores trigger colliders.
// Date: 2023-10-12
using UnityEngine;
public class Print : MonoBehaviour
{
public LayerMask layerMask; // Specify the layer mask.
public LayerMask excludedLayers; // Specify layers to be excluded.
void Update()
{
// Define a ray starting from the object's position and going forward.
Ray ray = new Ray(transform.position, transform.forward);
// Create a layer mask excluding specified layers.
int raycastLayerMask = layerMask & ~excludedLayers;
// Declare a variable to store information about the hit object.
RaycastHit hit;
// Check if the ray hits an object within the specified layer mask, ignoring triggers.
if (Physics.Raycast(ray, out hit, Mathf.Infinity, raycastLayerMask, QueryTriggerInteraction.Ignore))
{
// Log the name of the hit object.
Debug.Log("Raycast hit within the specified layer mask (excluding specified layers), ignoring triggers: " + hit.transform.name);
}
}
}
xxxxxxxxxx
// Raycast Example 10: Raycast with Custom Layer Mask and Self-Exclusion
// Description: This code performs a raycast from the object's position forward, using a custom layer mask and excluding the object's own layer.
// Date: 2023-10-12
using UnityEngine;
public class Print : MonoBehaviour
{
public LayerMask customLayerMask; // Specify a custom layer mask.
void Update()
{
// Define a ray starting from the object's position and going forward.
Ray ray = new Ray(transform.position, transform.forward);
// Create a layer mask with the custom layer mask, excluding the object's own layer.
int layerMask = customLayerMask & ~(1 << gameObject.layer);
// Declare a variable to store information about the hit object.
RaycastHit hit;
// Check if the ray hits an object within the custom layer mask, excluding self.
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
// Log the name of the hit object.
Debug.Log("Raycast hit within the custom layer mask (excluding self): " + hit.transform.name);
}
}
}