一个屑教程()
用户:
某只sans喵呀查看:3 回复:5 评论:3 创建时间:2022-05-13T16:31:16
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 }) => {//当一个实体碰撞到另一个实体时
-
- }) 来自
https://blog.csdn.net
回复
上一页第 1 页 / 共 1 页下一页