猫史档案馆


一个屑教程()

用户:某只sans喵呀某只sans喵呀查看:3 回复:5 评论:3 创建时间:2022-05-13T16:31:16


2.上载具实现

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

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

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

  1.   const originPlayerMesh = entity.mesh;//玩家
  2.   world.onInteract(({ entity, targetEntity }) => {
  3.   if (targetEntity.mesh.match("car") != 0) {
  4.   entity.mesh = targetEntity.mesh;//玩家模型变成车
  5.   entity.meshScale = targetEntity.meshScale;//大小一致
  6.   entity.meshOrientation = targetEntity.meshOrientation;
  7.   entity.position = targetEntity.position;//位置
  8.    
  9.   targetEntity.destroy();//原来的车销毁
  10.   entity.player.invisible = true;
  11.   }
  12.    
  13.   });

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

 

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

3.下载具实现

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

  1.   world.onPress(({ button, raycast }) => {
  2.   if (button === 'crouch') {
  3.   const origincar = entity;
  4.    
  5.   //车
  6.   world.createEntity({
  7.   id: "car",
  8.   mesh: origincar.mesh,
  9.   mass: origincar.mass,
  10.   friction: origincar.friction,
  11.   position: origincar.position,
  12.   meshScale: origincar.meshScale,
  13.   restitution: origincar.restitution,
  14.   meshOrientation: origincar.meshOrientation,
  15.   collides: true,
  16.   gravity: true,
  17.   fixed: true,
  18.   })
  19.   //world.createEntity(car);
  20.    
  21.   entity.mesh = originPlayerMesh
  22.   entity.player.invisible = false;//人物变为原来 注意顺序
  23.   }
  24.   })
4.完整代码实现
  1.   world.onPlayerJoin(({ entity }) => {//当玩家进入时
  2.   /
  3.   const originPlayerMesh = entity.mesh;//玩家
  4.   world.onInteract(({ entity, targetEntity }) => {
  5.    
  6.    
  7.   if (targetEntity.mesh.match("car") != 0) {
  8.   entity.mesh = targetEntity.mesh;//玩家模型变成车
  9.   entity.meshScale = targetEntity.meshScale;//大小一致
  10.   entity.meshOrientation = targetEntity.meshOrientation;
  11.   entity.position = targetEntity.position;//位置
  12.   targetEntity.destroy();//原来的车销毁
  13.   entity.player.invisible = true;
  14.   }
  15.    
  16.   });
  17.   ///上车
  18.   world.onPress(({ button, raycast }) => {
  19.   if (button === 'crouch') {
  20.   const origincar = entity;
  21.    
  22.   //车
  23.   world.createEntity({
  24.   id: "car",
  25.   mesh: origincar.mesh,
  26.   mass: origincar.mass,
  27.   friction: origincar.friction,
  28.   position: origincar.position,
  29.   meshScale: origincar.meshScale,
  30.   restitution: origincar.restitution,
  31.   meshOrientation: origincar.meshOrientation,
  32.   collides: true,
  33.   gravity: true,
  34.   fixed: true,
  35.   })
  36.   //world.createEntity(car);
  37.    
  38.   entity.mesh = originPlayerMesh
  39.   entity.player.invisible = false;//人物变为原来 注意顺序
  40.   }
  41.   })
  42.    
  43.   })
  44.   world.onEntityCreate(({ entity }) => {//此时的entity的模型为载具
  45.   if (entity.mesh.match("car") != 0) {
  46.   //console.log("是车");
  47.   entity.enableInteract = true; // 开启实体互动功能
  48.   entity.interactRadius = 8; // 互动范围大小
  49.   }
  50.   })
  51.   world.onEntityContact(({ entity, other }) => {//当一个实体碰撞到另一个实体时
  52.    
  53.   }) 来自

    https://blog.csdn.net


回复

上一页1 页 / 共 1下一页
某只sans喵呀某只sans喵呀

沙发

点赞0


评论


某只sans喵呀某只sans喵呀

啊啊啊啊啊啊啊,好冷

点赞0


评论


egg鸡蛋蛋egg鸡蛋蛋

(天花板

点赞0


评论


某只sans喵呀某只sans喵呀

还蛮好用的

点赞0


评论


堂主の犬堂主の犬

()

点赞0


评论