惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
C
Check Point Blog
博客园 - Franky
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
V2EX - 技术
V2EX - 技术
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Jina AI
Jina AI
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
H
Hackread – Cybersecurity News, Data Breaches, AI and More
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
Google Developers Blog
G
GRAHAM CLULEY
V
V2EX
The Register - Security
The Register - Security
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
Help Net Security
Help Net Security
大猫的无限游戏
大猫的无限游戏
C
CERT Recently Published Vulnerability Notes
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
S
Secure Thoughts
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
NISL@THU
NISL@THU
K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
宝玉的分享
宝玉的分享
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
博客园_首页
A
Arctic Wolf

博客园 - kingBook

git patch git 修改最后一次提交的日期 Win10 修改特定格式文件的右键快捷菜单 TypeScript async、 await、Promise LayaAir3.x 侦听程序退出 旋转力学例子 旋转力学公式 凹多边形碰撞检测 LayaAir3.x 侦听键盘事件 URP 阴影 TypeScript 里的 override TypeScript 类的自身类型 Unity 二维数组序列化 C# 匿名对象、动态属性 Cocos Creator 安卓模拟器中无法运行 Unity Editor 保存图片、缩放纹理 LayaAir3.2.0-beta.2 设置2d刚体线性速度,在不同设备(分辨率)下,表现不一致的问题 TypeScirpt 声明Map类型变量 TypeScript 声明函数类型变量
LayaAir3.x 物理2D碰撞事件
kingBook · 2024-07-26 · via 博客园 - kingBook

已挂载 Laya.RigidBody 的节点,可以直接挂载脚本并声明 onTriggerEnter、onTriggerStay、onTriggerExit 函数

const { regClass, property } = Laya;

@regClass()
export class PlayerBullet extends Laya.Script {

    declare owner: Laya.Sprite;
    private _body: Laya.RigidBody;
   
    onAwake(): void {
        this._body = this.owner.getComponent(Laya.RigidBody);
    }

    /** 3D物理触发器事件与2D物理碰撞事件,开始碰撞时执行 */
    onTriggerEnter(other: Laya.PhysicsColliderComponent | Laya.ColliderBase, self?: Laya.ColliderBase, contact?: any): void {
        let info = contact.getHitInfo();
        // 取消碰撞
        contact.SetEnabled(false);
        // 获取碰撞平面的法线
        console.log(info.normal.x info.normal.y);
    }
    
    /** 3D物理触发器事件与2D物理碰撞事件,持续碰撞时执行 */
    onTriggerStay(other: Laya.PhysicsColliderComponent | Laya.ColliderBase, self?: Laya.ColliderBase, contact?: any): void {
        
    }
        
    /** 3D物理触发器事件与2D物理碰撞事件,结束碰撞时执行 */
    onTriggerExit(other: Laya.PhysicsColliderComponent | Laya.ColliderBase, self?: Laya.ColliderBase, contact?: any): void {
        
    }

}

如果是挂载脚本的节点的子节点添加 Laya.RigidBody 组件,此时用:bodyNode.on(Laya.Event.TRIGGER_ENTER, ...) 方法, 如:

this.bodyNode.on(Laya.Event.TRIGGER_ENTER, this, this.onTriggerEnter);
this.bodyNode.on(Laya.Event.TRIGGER_STAY, this, this.onTriggerStay);
this.bodyNode.on(Laya.Event.TRIGGER_EXIT, this, this.onTriggerExit);

// 不用时,使用 off() 移除侦听
//this.bodyNode.off(Laya.Event.TRIGGER_ENTER, this, this.onTriggerEnter);
//this.bodyNode.off(Laya.Event.TRIGGER_STAY, this, this.onTriggerStay);
//this.bodyNode.off(Laya.Event.TRIGGER_EXIT, this, this.onTriggerExit);

onTriggerEnter(other: Laya.PhysicsColliderComponent | Laya.ColliderBase, self?: Laya.ColliderBase, contact?: any):void{
    console.log(contact);
}

onTriggerStay(other: Laya.PhysicsColliderComponent | Laya.ColliderBase, self?: Laya.ColliderBase, contact?: any): void {
    
}

onTriggerExit(other: Laya.PhysicsColliderComponent | Laya.ColliderBase, self?: Laya.ColliderBase, contact?: any): void {
    
}