用户:
屑JOJO查看:0 回复:0 评论:0 创建时间:2022-12-22T21:50:56
下面这段屑码如何解蕨屑射⚦角问题(蓝瘦
//完整代码
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.addTag('子弹打不到');
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})=>{
if(button=='action0'){
while(entity.player.action0Button){
shoot(entity);
await sleep(200);
};
};
});
(async function(){
while(1){
world.querySelectorAll('.子弹').forEach((e)=>{
var up = Math.atan2(e.velocity.y,Math.sqrt(e.velocity.x**2+e.velocity.z**2))
//047g喵代码(?)
var angle = Math.atan2(e.velocity.z,e.velocity.x)
e.meshOrientation=new Box3Quaternion(0,0,0,1).rotateY(angle).rotateX(up);//仰角
var raycast = world.raycast(e.position,
new Box3Vector3(
Math.cos(angle)*Math.cos(up),
Math.sin(up),
Math.sin(angle)*Math.cos(up)
),{ignoreSelector:'.子弹打不到',maxDistance:10});//重难点!!!子弹打不到是一个标签,最好加在子弹和玩家实体上!
if(raycast.hitVoxel!=0){//有打中方块!然后可以销毁什么的)))
}
if(raycast.hitEntity){//击中实体...进行damage操作
}
})
await sleep(100);//检测间隔100~
}
})()