【神奇代码岛】角色上下载具与操控载具效果实现 利用交互事件与实体创建/销毁事件(编程猫 Box3平台)

完整代码实现在文末,整体效果如下,可以多次上下载具:

1.为载具开启互动功能

        上载具的交互方式选用Box3平台自带的互动事件world.onInteract()。在神奇代码岛中,载具以实体(模型)的形式出现。在所有实体被初始化的时候,找出所有游戏中作为载具的实体,将他们的“实体交互功能”设置为有效。如下所示(此处使用mesh的名字是否有“car”的方式判断是否为载具):

world.onEntityCreate(({ entity }) => {
    if (entity.mesh.match("car") != 0) {
        //console.log("是车");
        entity.enableInteract = true;   // 开启实体互动功能
        entity.interactRadius = 8;     // 互动范围大小
    }
})

下载具使用"c"键。

2.上载具实现

        上载具的具体逻辑分为以下几步:

        1.将玩家实体Player保存,留作备份。2.将玩家的模型换成载具

        3.设置此时玩家的参数 4.将原来载具的实体销毁 将玩家设为可见

const originPlayerMesh = entity.mesh;//玩家
    world.onInteract(({ entity, targetEntity }) => {
        if (targetEntity.mesh.match("car") != 0) {
            entity.mesh = targetEntity.mesh;//玩家模型变成车
            entity.meshScale = targetEntity.meshScale;//大小一致
            entity.meshOrientation = targetEntity.meshOrientation;
            entity.position = targetEntity.position;//位置

            targetEntity.destroy();//原来的车销毁
            entity.player.invisible = true;
        }

    });

        此时的逻辑是玩家上了载具,玩家的人物消失,取而代之的是载具的模型。故此时玩家的大小、位置、旋转方位应该与载具一致,即meshScale、meshOrientation、position等几个参数。

         特别注意:要在地图中使用自带的工具调整载具模型的初始朝向(旋转角度),使载具的正方向与模型的正向平齐,否则上载具之后会发现车斜着开的效果。(可以一点点试)

3.下载具实现

        下载具的逻辑如下:人物离开载具,恢复回人形的模型,并丢下一辆载具。可以先在当前玩家的位置创建一个实体,实体为正在开的载具,载具的各项参数应该与原载具一致。然后将玩家的模型替换为初始状态(此处使用到了前面保存的originPlayerMesh)。最后设置可见。代码如下:

world.onPress(({ button, raycast }) => {
        if (button === 'crouch') {
            const origincar = entity;

            //车
            world.createEntity({
                id: "car",
                mesh: origincar.mesh,
                mass: origincar.mass,
                friction: origincar.friction,
                position: origincar.position,
                meshScale: origincar.meshScale,
                restitution: origincar.restitution,
                meshOrientation: origincar.meshOrientation,
                collides: true,
                gravity: true,
                fixed: true,
            })
            //world.createEntity(car);

            entity.mesh = originPlayerMesh
            entity.player.invisible = false;//人物变为原来 注意顺序
        }
    })

4.完整代码实现

world.onPlayerJoin(({ entity }) => {//当玩家进入时
    /
    const originPlayerMesh = entity.mesh;//玩家
    world.onInteract(({ entity, targetEntity }) => {


        if (targetEntity.mesh.match("car") != 0) {
            entity.mesh = targetEntity.mesh;//玩家模型变成车
            entity.meshScale = targetEntity.meshScale;//大小一致
            entity.meshOrientation = targetEntity.meshOrientation;
            entity.position = targetEntity.position;//位置
            targetEntity.destroy();//原来的车销毁
            entity.player.invisible = true;
        }

    });
    ///上车
    world.onPress(({ button, raycast }) => {
        if (button === 'crouch') {
            const origincar = entity;

            //车
            world.createEntity({
                id: "car",
                mesh: origincar.mesh,
                mass: origincar.mass,
                friction: origincar.friction,
                position: origincar.position,
                meshScale: origincar.meshScale,
                restitution: origincar.restitution,
                meshOrientation: origincar.meshOrientation,
                collides: true,
                gravity: true,
                fixed: true,
            })
            //world.createEntity(car);

            entity.mesh = originPlayerMesh
            entity.player.invisible = false;//人物变为原来 注意顺序
        }
    })

})
world.onEntityCreate(({ entity }) => {//此时的entity的模型为载具
    if (entity.mesh.match("car") != 0) {
        //console.log("是车");
        entity.enableInteract = true;   // 开启实体互动功能
        entity.interactRadius = 8;     // 互动范围大小
    }
})
world.onEntityContact(({ entity, other }) => {//当一个实体碰撞到另一个实体时

})

5.注意

1.点击空格汽车可以飞天的现象依旧有,不过神奇代码岛上其他类似的游戏也有这个现象。

2.注意在地图中调整车的旋转朝向,否则会出现车的侧面为车头然后向前运动的现象

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值