猫史档案馆


【box3】火箭炮系统-代码全公开

用户:YDS尹子YDS尹子查看:14 回复:17 评论:14 创建时间:2021-01-31T20:02:22


使用指南

对准方块或实体按下左键,喵会在大约5秒内抵达目标

准确度:

10格内:完全准确

操作指南

首先,放置一个名为"舰载激光炮"的模型,并添加标签"roclauncher".

其次,放置一个名为"鱼雷-火箭"的模型(这个模型是我拿鱼雷模型改的),并添加标签"roc".

然后,添加以下代码:

const Quat = new Box3Quaternion(0, 1, 0, 0)// box引擎默认的旋转朝向
const Quat2 = new Box3Quaternion(0, 1, 0, 0)// box引擎默认的旋转朝向
var roc = world.querySelector(".roc")
roc.meshOrientation.set(0, 1, -1, 0)

var readyTime=0.5;//装弹时间-秒
var flyTime=5;//飞行时长-秒
world.querySelectorAll(".roclauncher").forEach(async (e) => {
    e.ready=true;//装弹完毕
})
world.onPress(({ entity, button, raycast: { hitPosition } }) => {
    if (button == 'action1') {
        // entity.velocity.y+=0.2;
        // var direction = e.position.sub(hitPosition);
        world.querySelectorAll(".roclauncher").forEach(async (e) => {
            
            if(e.ready){
                e.ready=false;
            console.log(e.id)
            var direction = hitPosition.sub(e.position);
            var orientation = Quat.rotateY(Math.atan2(direction.z, direction.x))
            var orientation2 = Quat2.rotateY(Math.atan2(direction.z, direction.x))
            e.meshOrientation.copy(orientation)
            var crted = world.createEntity();
            crted.ctrl = false;
            crted.meshOrientation.copy(orientation2);
            crted.position.set(e.position.x, e.position.y, e.position.z);
            crted.mesh = roc.mesh;
            crted.meshScale = roc.meshScale;
            crted.gravity = true; crted.fixed = false;
            crted.launcher = e;
            crted.addTag("lchr");

            crted.mass = 1;


            crted.particleLimit = 1000;
            crted.particleRate = 1000;
            crted.particleColor = [new Box3RGBColor(1, 1, 0), new Box3RGBColor(1, 0, 0)]
            crted.particleSize = [5, 4]
            crted.particleAcceleration.set(0, -5, 0)
            crted.particleVelocitySpread.set(5, 1, 5);
            crted.particleLifetime = 2;

            crted.gravity = false;
            crted.velocity.y = 2;
            for (let i = 0; i < 10; i++) {
                crted.velocity.x += direction.x / 200;
                crted.velocity.z += direction.z / 200;
                await sleep(30);
            }
            crted.tar = hitPosition;
            crted.ctrl = true;
            setTimeout(()=>{e.ready=true;},readyTime*1000)
            setTimeout(() => { crted.destroy() }, flyTime*1000)
            }else{
                entity.player.directMessage("火箭炮装弹中...")
            }
        })

    }

})
world.onVoxelContact(async ({ entity, x, y, z }) => {
    if (entity.hasTag("lchr")) {
        entity.particleVelocity.set(0, 30, 0);
        await sleep(500);
        entity.destroy()
    }
})

world.onTick(() => {
    world.querySelectorAll(".lchr").forEach((e) => {
        var orientation2 = Quat2.rotateY(Math.atan2(e.velocity.z, e.velocity.x))
        e.meshOrientation.copy(orientation2);
        e.meshOrientation.y = -Math.atan2(e.velocity.y, Math.sqrt(e.velocity.x * e.velocity.x + e.velocity.z * e.velocity.z))
        e.particleVelocity.set(-e.velocity.x, -e.velocity.y, -e.velocity.z);
        if (e.ctrl) {
            var direction = e.tar.sub(e.position);
            e.velocity.x = (direction.x+direction.x/2) / 20;
            e.velocity.y -= 0.1;
            e.velocity.z = (direction.z+direction.z/2) / 20;
        }
    })
})


回复

上一页1 页 / 共 1下一页
YDS尹子YDS尹子

被催着发布没来得及放图片(捂脸

点赞2


评论


GZ006GZ006

沙发

点赞0


评论


小树同学小树同学

我干脆给自己搞个” YDS尹子每个干货打卡emotion_偷笑

点赞1


评论


IaeaIaea

666

点赞0


评论


指令者指令者

666

点赞0


评论


面具狐狸云绾兮面具狐狸云绾兮

尹子你有穿戴物件的代码吗,我不会QAQ

点赞0


评论


面具狐狸云绾兮面具狐狸云绾兮

https://shequ.codemao.cn/community/364476看看这个帖子QAQ

点赞0


评论


永远的ICE永远的ICE

伊子,你可以帮我弄一个烟花的代码吗

点赞0


评论


幽梦子曦幽梦子曦

你的代码又有问题了,是右键吧

点赞0


评论


指令者指令者

6

6

6

点赞0


评论


IaeaIaea

打卡

点赞0


评论


JYF_斗罗JYF_斗罗

牛~

点赞0


评论


冰糖葫芦_冰糖葫芦_

收藏

点赞0


评论


该账号已停用该账号已停用

emotion_编程猫_厉害了

点赞0


评论


全网一个白虎兄全网一个白虎兄

emotion_编程猫_点赞emotion_编程猫_厉害了

点赞0


评论


憨白不憨awa憨白不憨awa

()()()

点赞0


评论


夜影行者夜影行者

收藏

点赞0


评论