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;
    }
相关推荐
何时梦醒7 分钟前
深入理解递归与快速排序 —— 从基础入门到手写实现
前端·javascript
bonechips18 分钟前
LLM 的无状态:从 HTTP 协议到对话上下文工程
前端·javascript
胡志辉19 分钟前
从 prototype 到 V8,看懂 JavaScript 原型链
前端·javascript
ping某2 小时前
专栏-null 和 undefined 到底是什么?
前端·javascript·后端
swipe5 小时前
从 0 到 1 理解 React 虚拟列表:定高、不定高与 Canvas 版本完整拆解
前端·javascript·面试
铁皮饭盒5 小时前
Bun执行python代码
前端·javascript·后端
zzzzzz3107 小时前
当甲方说'logo放大的同时再缩小一点'时,我用 AI 把这个需求做出来了
javascript·css·程序员
Hilaku7 小时前
Node.js 还能再战十年?给你一个不换引擎的理由
前端·javascript·程序员
weedsfly8 小时前
前端必知必会:从 IIFE 到 ESM,模块化到底在解决什么?
前端·javascript
渣波8 小时前
拒绝 SQL 焦虑!手把手带你用 NestJS + Prisma + DTO 写出“防弹”级后端代码
javascript·数据库·后端