重写全局 Physics.queriesHitTriggers 以指定默认情况下查询(射线投射、球形投射、重叠测试等)是否命中触发器。 对查询使用 Ignore 可忽略触发碰撞体。
//Create two GameObjects (e.g. a Cube) and place them near each other. Attach this script to one of them. //Click on the GameObject with the script. Attach the other GameObject to the “My Game Object” field in the Inspector. //Make sure both have Collider components //Choose your own “Max Distance” in the Inspector (e.g. 600).
//This script casts a ray that ignores Trigger Colliders. //Press space to switch the second GameObject between a Trigger and non-Trigger GameObject. When the Trigger is off, the ray detects a collision. When it is on, no collisions occur.
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; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.