用户:
凌_墨水查看:6 回复:8 评论:6 创建时间:2021-10-05T15:55:06
我是来水帖的
如题
实体与玩家碰撞关闭
玩家如何在进入实体时触发事件呢?
其实
这个问题早就在模板中被解决了
只不过模板代码有点难懂
没人看罢了
核心原理如下:
/*
1.计算实体xyz轴的lo值和hi值,并存储入变量,也就是构造实体边界框
2.设置区域检测,lo值是lo值,hi值是hi值
3.玩家触发区域
*/
整段代码不长
只不过有些计算问题难以解决
如计算实体边界框
不过
这个在模板中有对应的函数
使我们能直接解决这个问题
代码展示
function getEntityBounds(entity) {//计算边界框函数
const lo = entity.position.sub(entity.bounds);
const hi = entity.position.add(entity.bounds);
const bounds = new Box3Bounds3(lo, hi);
return bounds;
}
world.querySelectorAll('.233').forEach((e) => {
const bounds = getEntityBounds(e); // 获取实体边界框,并存储入变量
const area = world.addZone({//区域检测
selector: 'player',
bounds: {
lo: bounds.lo,
hi: bounds.hi
},
})
area.onEnter(({ entity }) => {//当玩家进入区域
entity.velocity.y += 1//跳一下
});
})
还是模板好代码多)
那么关于实体进入触发器的教程
就是这样了
拜拜-.-