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;
    }
相关推荐
知识分享小能手33 分钟前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
luckys.one34 分钟前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
mCell1 小时前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
weixin_437830943 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
gnip4 小时前
JavaScript事件流
前端·javascript
小菜全4 小时前
基于若依框架Vue+TS导出PDF文件的方法
javascript·vue.js·前端框架·json
wow_DG4 小时前
【Vue2 ✨】Vue2 入门之旅 · 进阶篇(一):响应式原理
前端·javascript·vue.js
weixin_456904274 小时前
UserManagement.vue和Profile.vue详细解释
前端·javascript·vue.js
秋秋小事4 小时前
React Hooks useContext
前端·javascript·react.js
ST.J4 小时前
前端笔记2025
前端·javascript·css·vue.js·笔记