Legacy Documentation: Version 5.2
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Physics.IgnoreCollision

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public static function IgnoreCollision(collider1: Collider, collider2: Collider, ignore: bool = true): void;
public static void IgnoreCollision(Collider collider1, Collider collider2, bool ignore = true);
public static function IgnoreCollision(collider1: Collider, collider2: Collider, ignore: bool = true): void;
public static void IgnoreCollision(Collider collider1, Collider collider2, bool ignore = true);

Parameters

Description

Makes the collision detection system ignore all collisions between collider1 and collider2.

This is useful, say, for preventing projectiles from colliding with the object that fires them.

Note that IgnoreCollision will reset the trigger state of affected colliders, so you might receive OnTriggerExit and OnTriggerEnter messages in response to calling this.

IgnoreCollision has a few limitations: 1) It is not persistent. This means ignore collision state will not be stored in the editor when saving a scene. 2) You can only apply the ignore collision to colliders in active game objects. When deactivating the collider or attached rigidbody the IgnoreCollision state will be lost and you have to call Physics.IgnoreCollision again. See Also: Physics.IgnoreLayerCollision.

// Instantiate a bullet and make it ignore collisions with this object.

var bulletPrefab : Transform;

function Start () { var bullet = Instantiate(bulletPrefab) as Transform; Physics.IgnoreCollision(bullet.GetComponent.<Collider>(), GetComponent.<Collider>()); }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform bulletPrefab; void Start() { Transform bullet = Instantiate(bulletPrefab) as Transform; Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>()); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961