Описание
Запросы никогда не сообщают о совпадениях триггеров.
//Создаем два игровых объекта (например, куб) и размещаем их рядом друг с другом. Прикрепите этот скрипт к одному из них.
//Нажмите на GameObject со скриптом. Прикрепите другой GameObject к полю «Мой игровой объект» в Инспекторе.
// Убедитесь, что у обоих есть компоненты Collider
// Выберите собственное «Максимальное расстояние» в Инспекторе (например, 600).
//Этот скрипт испускает луч, который игнорирует триггерные коллайдеры.
//Нажмите пробел, чтобы переключить второй GameObject между триггером и не-триггером GameObject. Когда триггер выключен, луч обнаруживает столкновение. Когда он включен, столкновений не происходит.
using UnityEngine;
public class Example : MonoBehaviour
{
//The maximum distance from your GameObject. Make sure to set this in the Inspector
public float m_MaxDistance;
public LayerMask m_Mask = -1;
//Assign a GameObject in the Inspector that you want to test collisions with
public GameObject m_MyGameObject;
//This is the Collider of the GameObject you assign in the Inspector
Collider m_OtherGameObjectCollider;
void Start()
{
//Fetch the Collider from the GameObject you assign in the Inspector
m_OtherGameObjectCollider = m_MyGameObject.GetComponent<Collider>();
}
void FixedUpdate()
{
//Set the direction as forward
Vector3 direction = transform.TransformDirection(Vector3.forward);
//Use Physics to calculate the raycast
//Uses your GameObject's original position, the direction (above), the max distance from your GameObject, and the LayerMask value to calculate raycast.
//Also tells it to ignore trigger colliders using QueryTriggerInteraction
if (Physics.Raycast(transform.position, direction, m_MaxDistance, m_Mask.value, QueryTriggerInteraction.Ignore))
print("There is something in front of the object!");
}
void Update()
{
//Press space to turn the other GameObject's trigger status on and off
if (Input.GetKeyDown(KeyCode.Space))
{
//Test if the trigger collisions are ignored by turning the GameObject's trigger collider on and off
m_OtherGameObjectCollider.isTrigger = !m_OtherGameObjectCollider.isTrigger;
}
}
}