猫史档案馆


【box3】如何发射子弹

用户:就在天边等待你就在天边等待你查看:3 回复:5 评论:3 创建时间:2023-11-09T12:48:17


我正在做一个多人射击游戏,怎么让玩家按下左键发射子弹?


回复

上一页1 页 / 共 1下一页
Dang的猫猫Dang的猫猫

你自己弄变量去呗

点赞0


评论


Dang的猫猫Dang的猫猫

world.onPress(async ({ entity, raycast }) => {
    let e = world.createEntity({
        position: entity.position.add(new Box3Vector3(raycast.direction.x, raycast.direction.y, raycast.direction.z)),//生成位置
        mesh: `mesh/投掷物名称.vb`,//投掷物名称
        gravity: false,//取消下落
        collides: true,//开启碰撞
        meshScale: new Box3Vector3(0.05, 0.05, 0.05),//大小
        meshOrientation: new Box3Quaternion(0,0,0,1).rotateY( Math.atan2( raycast.direction.z, raycast.direction.x)).rotateY(Math.PI / 2),
        //按照MC那样,不考虑上下的旋转,我的模型是X X 1的,如果是1 X X的不用加第二个rotateY(应该吧我也没试过)
        velocity: raycast.direction.mul(new Box3Vector3(2, 2, 2)),//设置velocity,mul里的数字随便搞,114514都可以()
        fixed: false,//因为有velocity,所以要让实体能动起来
        friction: 1.5,//也是,随便选,推荐1.2+,2-,这个期间的实体碰到东西后,会停止(或者向前滑行一段,大约1.4+(含1.4)会回弹一段())
    })
})

点赞0


评论


Dang的猫猫Dang的猫猫

这是我自己做的投射代码达不到你那种程度但是也是可以发射

点赞0


评论


Dang的猫猫Dang的猫猫

这个变量你就自己挑吧

点赞0


评论


这个屑水烨不会写代码这个屑水烨不会写代码

代码如下:(注:模型要和模型名字一样,不然会报错)

world.addCollisionFilter('player', '.子弹')
//首先先导入一个子弹的模型,为0,0,0,1的默认角度
//子弹创建代码:
console.clear()
async function shoot(entity = new GameEntity) {
    var speed = 5;//越大越快
    const e = world.createEntity({
        mesh: 'mesh/子弹.vb',//其实未必要子弹,弓箭啥一样的
        meshScale: new GameVector3(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 GameQuaternion(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(5000);
    //10秒后消失,防止太多飞出边界
    e.destroy()

}

world.onPress(async ({ entity, button, raycast }) => {
    if (button == GameButtonType.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 GameQuaternion(0, 0, 0, 1).rotateX(
                up
            ).rotateY(
                angle
            )
            e.velocity.y -= 0.1//重力下垂
            var hitBox = new GameBounds3(e.position.sub(e.bounds), e.position.add(e.bounds));
            var hitEntities = world.querySelectorAll('*').filter(a => hitBox.intersects(new GameBounds3(a.position.sub(a.bounds), a.position.add(a.bounds))))
            for (let entity of hitEntities) {
                e.onEntityContact(({ other }) => {
                    if (other.isPlayer) {
                        Die(other)
                    }
                })
            }
        })
        await sleep(100)
    }
})()

点赞0


评论