猫史档案馆


【神奇代码岛】上下车代码教程

用户:热情的小朋友热情的小朋友查看:2 回复:2 评论:2 创建时间:2023-05-16T19:49:58


嘿嘿!我是热情的小朋友!

我又带来新教程啦!就是上下车的代码,代码见下面:

//首先要在场景中放入一辆汽车模型,并给模型加上一个标签,叫:“车”
//本教程注释挺多,你别说你看不懂。
for (const e of world.querySelectorAll('.车')) {//遍历所有实体
    e.collides = true //让车模型开启碰撞
    e.fixed = true //使车模型固定实体不被推移
    e.onEntityContact(async ({ other }) => { //每当车碰到另一个实体
        if (other.isPlayer) { //如果另一个实体是玩家
            other.mesh = e.mesh;
            other.meshScale = e.meshScale;
            other.meshOrientation = e.meshOrientation.rotateY(Math.PI/0.5);// 调整mesh方向, 旋转180度,你可以根据你的运行结果调整此代码。
            other.player.invisible = true; // 隐藏玩家模型(敞篷车不用隐藏)
            // 设置速度:
            other.player.walkSpeed = 0.8;
            other.player.walkAcceleration = 0.05;
            other.player.runSpeed = 2.2;
            other.player.runAcceleration = 0.08;
            other.position.set(31,30,226)
        }
    })
}
world.onChat(async({ entity, message }) => {//每当玩家在聊天窗口输入信息
    if (message == '下车') {//如果输入信息为:“下车”
        entity.player.shininess = 0;
        entity.player.walkSpeed = 0.22;
        entity.player.walkAcceleration = 0.19;
        entity.player.runAcceleration = 0.35;
        entity.player.runSpeed = 0.4;
        entity.player.flySpeed = 0.5;
        entity.player.swimSpeed = 0.5;
        entity.player.crouchSpeed = 0.2;
        entity.player.jumpPower = 0.96;
        entity.player.doubleJumpPower = 0.9;
        entity.mesh = entity.originPlayerMesh;
        entity.player.invisible = false;// 显示玩家模型
        setTimeout(() => {
            console.log('cancel tick handler');//输出下车提示
        }, 1);  
    };
});
function getEntityBounds(entity) {
    const lo = entity.position.sub(entity.bounds);
    const hi = entity.position.add(entity.bounds);
    const bounds = new Box3Bounds3(lo, hi);
    return bounds;
}
//以上是本教程的全部

 


回复

上一页1 页 / 共 1下一页
USS繁星USS繁星

沙发

点赞0


评论


热情的小朋友热情的小朋友

嘿嘿,大家都别抢了。。

emotion_编程猫_嗨起来

点赞0


评论