开头引入
一款好的游戏需要一些玩家碰撞之后产生一些交互,如果一直使用模型对话产生交互的话,这就太麻烦了,于是,我告诉大家一个新型交互,这就是碰撞。
上课
![](https://img-blog.csdnimg.cn/img_convert/e90030fd4e8a1e23b11b94889863482a.gif)
正文
在碰撞交互里面有两种交互方式,1.方块交互;2.模型交互
方块交互(代码模板)
world.onVoxelContact(({entity,voxel,x,y,z,force,axis,tick})=>{}) //开始碰撞会调用的函数
world.onVoxelSeparate(({entity,voxel,x,y,z,force,axis,tick})=>{})//结束碰撞会调用的函数
准备阶段
先在地上放一个名为“stone”的方块
![](https://img-blog.csdnimg.cn/img_convert/77ce71b211dc430408a7d7889d0f9617.png)
上代码
普通碰撞代码
world.onVoxelContact(({entity,voxel})=>{
const voxelName = voxels.name(voxel) //将方块赋值给常量voxelName,以方便我们调用或者指定玩家碰到的方块
if (voxelName === "stone"){ //判断实体踩到的方块是否为stone
entity.player.directMessage("你踩到了一个名为“stone”的方块") // 如果踩到了会触发的交互
}
})
检测脚下的碰撞代码
world.onVoxelContact(({entity,voxel,axis})=>{
const voxelName = voxels.name(voxel) //将方块赋值给常量voxelName,以方便我们调用或者指定玩家碰到的方块
if (voxelName === "stone" && axis === 1){ //判断实体踩到的方块是否为stone
entity.player.directMessage("你踩到了一个名为“stone”的方块") // 如果踩到了会触发的交互
}
})
参数查看
开始碰撞
![](https://img-blog.csdnimg.cn/img_convert/6515636bd9f30aa9d8001fb1757d89cd.png)
结束碰撞
![](https://img-blog.csdnimg.cn/img_convert/56b3e11587923367254c25f58f33ad40.png)
实体交互(代码模板)
world.onEntityContact(({entity,other,force,axis,tick})=>{})
world.onEntitySeparate(({entity,other,force,axis,tick})=>{})
准备阶段
![](https://img-blog.csdnimg.cn/img_convert/60553535b7c31418c6bd88c84f2f01c9.png)
上代码
world.onEntityContact(({entity,other})=>{//entity一般指玩家碰到的实体,other一般指玩家
if (entity.id == "bb"){ //指定碰撞的模型
other.player.directMessage("蔡徐坤好帅")
}
})
参数查看
开始碰撞
![](https://img-blog.csdnimg.cn/img_convert/8edcbc61a67ea8ab3ed28e5cd5424c31.png)
结束碰撞
![](https://img-blog.csdnimg.cn/img_convert/26c567920e31e03b9d1bdbf14696a674.png)
结语
这节课就到这里了,下课
![](https://img-blog.csdnimg.cn/img_convert/c37c51fb579c07f36146d7bcf69da46d.gif)