返回介绍

Physics.IgnoreCollision 忽略碰撞

发布于 2019-12-18 15:38:16 字数 2773 浏览 896 评论 0 收藏 0

JavaScript => public static function IgnoreCollision(collider1: Collider, collider2: Collider, ignore: bool = true): void;
C# => public static void IgnoreCollision(Collider collider1, Collider collider2, bool ignore = true);

Description 描述

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

使碰撞检测系统忽略所有collider1和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将重置受影响的碰撞器的触发状态,因此你可能收到OnTriggerExit和OnTriggerEnter的响应消息。

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.

IgnoreCollision有一些局限性:
1) 不具有持续性,这意味着忽略的碰撞状态当场景保存时,在编辑器中不会被储存。
2) 你只能在运行时应用忽略碰撞到碰撞器。当禁用该碰撞器或刚体,IgnoreCollision状态将丢失,你必须再次调用Physics.IgnoreCollision。

JavaScript:

// 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>());
}

C#:

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>());
    }
}

Physics

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文