用户:
全能代码师查看:18 回复:25 评论:18 创建时间:2022-07-14T12:34:05
world.addCollisionFilter('player','.子弹')
//首先先导入一个子弹的模型,为0,0,0,1的默认角度
//子弹创建代码:
console.clear()
async function shoot(entity = new Box3Entity) {
var speed = 5;//越大越快
const e = world.createEntity({
mesh: 'mesh/子弹.vb',//其实未必要子弹,弓箭啥一样的
meshScale: new Box3Vector3(0.0625, 0.0625, 0.0625),//自己调整
gravity: false, //通过代码控制,而不是使用自带的
collides: true,//也是使用代码控制,防止打到墙壁会反弹,使用代码还可以有一点穿透效果
position: entity.position,
})
e.speed = speed
e.own = entity
e.addTag('子弹')
var angle = Math.atan2(entity.player.facingDirection.z, entity.player.facingDirection.x)
//敲重点:
e.velocity.set(
Math.cos(angle) * Math.cos(-entity.player.cameraPitch) * speed,
Math.sin(-entity.player.cameraPitch) * speed,
Math.sin(angle) * Math.cos(-entity.player.cameraPitch) * speed,
)
e.meshOrientation = new Box3Quaternion(0, 0, 0, 1).rotateX(
Math.atan2(e.velocity.y, Math.sqrt(e.velocity.x ** 2 + e.velocity.z ** 2))
).rotateY(angle)
e.onVoxelContact(()=>{
e.destroy()
})
await sleep(10000);
//10秒后消失,防止太多飞出边界
e.destroy()
}
world.onPress(async ({ entity, button, raycast }) => {
if (button == Box3ButtonType.ACTION0) {
while (entity.player.action0Button) {//连射
shoot(entity)
await sleep(100)//连射间隔,自己定
}
}
});
(async function () {//主循环
while (1) {
world.querySelectorAll('.子弹').forEach((e) => {
var angle = Math.atan2(e.velocity.z, e.velocity.x)
var up = Math.atan2(e.velocity.y, Math.sqrt(e.velocity.x ** 2 + e.velocity.z ** 2))
e.meshOrientation = new Box3Quaternion(0, 0, 0, 1).rotateX(
up
).rotateY(
angle
)
e.velocity.y -= 0.05//重力下垂
var hitBox = new Box3Bounds3(e.position.sub(e.bounds), e.position.add(e.bounds));
var hitEntities = world.querySelectorAll('*').filter(a => hitBox.intersects(new Box3Bounds3(a.position.sub(a.bounds), a.position.add(a.bounds))))
for (let entity of hitEntities) {
//自己写!
}
})
await sleep(100)
}
})()
喜爱的话记得一键三连(滑稽)
ZzhAllene.skill1=0
while (e.player.action0Button&&!e.skill1) {
e.skill1=1
//await .....
e.skill1=0
}
//这个能解决连按多次左键之后再按住会多发射子弹的bug()()点赞0
评论
安静的蓝甲虫2knXimgsr喵demao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"
点赞0
评论
imgsrc="https://static.cod喵/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E5%8E%89%E5%AE%B3%E4%BA%86.gif"alt="emotion_编程猫_厉害了"
点赞0
评论