Laya3d碰撞后退表现算法

1.3D精灵碰撞,后退表现:

(1)定义后退移动的开关,速度向量,移动时间间隔

javascript 复制代码
        var vec3 = new Laya.Vector3();
        
        //后退移动速度倍率
        this.strikeBackSpeed = 12;
        //后退持续时间
        this.strikeDuration = 200;
        //是否正在后退
        this._isStriking$ = false;
        //后退移动速度向量
        this._strikingCurrentMovingSpeed$ = new Laya.Vector3();

(2)发生碰撞时,触发碰撞后退:

javascript 复制代码
    /** 与同等级的恐龙碰撞弹开 */
    strikeBack(otherDinosaur) {                   //碰撞它的3D精灵
        if (this._isStriking$) return;
        this.owner.transform.getForward(vec3);   //被碰撞的3D精灵的位置
        if (otherDinosaur) {
            let curPos = this.owner.transform.position;
            let dir = new Laya.Vector3();
            Laya.Vector3.subtract(otherDinosaur.owner.transform.position, curPos, dir);
            let forward = new Laya.Vector3();
            Laya.Vector3.scale(vec3, -1, forward);//通过实验得知获取的向量反向为前进的向量
            if (Laya.Vector3.dot(dir, forward) < 0) return;
        }
        Laya.Vector3.scale(vec3, this.strikeBackSpeed, this._strikingCurrentMovingSpeed$);
        this._isStriking$ = true;
        this.owner.timerOnce(this.strikeDuration, this, () => {
            this._isStriking$ = false;
        });
    }

(3)onUpdate中更新位置:

javascript 复制代码
onUpdate() {
         if (this._isStriking$) {
             let e = Math.min(Laya.timer.delta * 0.001, 1 / 30);
             this._strikingBackMove$(e);
         }
    }

(4)实际发生位移的函数:

javascript 复制代码
    /**
     * 和同等级的恐龙碰撞弹开
     * @param {*} e 缩放值
     */
    _strikingBackMove$(e) {
        let transform = this.owner.transform;
        Laya.Vector3.scale(this._strikingCurrentMovingSpeed$, e, vec3);
        Laya.Vector3.add(transform.position, vec3, vec3);
        transform.position = vec3;
    }
相关推荐
玖玥拾6 小时前
Unity3D RPG 入门项目(十一)主角完整战斗、怪物自动刷新、四大类型技能系统
游戏·3d·unity·游戏引擎
冬夜戏雪10 小时前
agent的trace 和eval
开发语言·前端·javascript
感谢一路走过的人11 小时前
AGGrid刷新数据后执行筛选
javascript·ag-grid
水獭比特11 小时前
工具都批准了,为什么还不能执行?给 Agent 补上第二道校验
javascript·人工智能·node.js
夏雪coding12 小时前
JeecgBoot 动态路由踩坑(Vue2):刷新白屏、permissionList[0] 报 undefined,以及我最后为什么全删了
javascript·vue.js
默_笙12 小时前
😋 我让爬虫终于看到了我的网站,后端同事说"这也行?"(上):SEO 与渲染模式
前端·javascript
七牛开发者14 小时前
解读 Cordis:dsh 一切皆插件背后的运行时设计
前端·javascript·后端
雷工笔记14 小时前
KingFusion驾驶舱-产能信息模块开发实战
javascript
敢敢のwings15 小时前
世界模型与FlashWorld:从“在梦中学习“到秒级3D场景生成
android·学习·3d