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;
    }
相关推荐
东东5161 小时前
智能社区管理系统的设计与实现ssm+vue
前端·javascript·vue.js·毕业设计·毕设
catino1 小时前
图片、文件的预览
前端·javascript
mocoding2 小时前
Flutter 3D 翻转动画flip_card三方库在鸿蒙版天气预报卡片中的实战教程
flutter·3d·harmonyos
2501_920931702 小时前
React Native鸿蒙跨平台实现推箱子游戏,完成玩家移动与箱子推动,当所有箱子都被推到目标位置时,玩家获胜
javascript·react native·react.js·游戏·ecmascript·harmonyos
AI老李3 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·postcss
方也_arkling3 小时前
Element Plus主题色定制
javascript·sass
2601_949809594 小时前
flutter_for_openharmony家庭相册app实战+我的Tab实现
java·javascript·flutter
Up九五小庞4 小时前
开源埋点分析平台 ClkLog 本地部署 + Web JS 埋点测试实战--九五小庞
前端·javascript·开源
摘星编程4 小时前
React Native + OpenHarmony:UniversalLink通用链接
javascript·react native·react.js
qq_177767374 小时前
React Native鸿蒙跨平台数据使用监控应用技术,通过setInterval每5秒更新一次数据使用情况和套餐使用情况,模拟了真实应用中的数据监控场景
开发语言·前端·javascript·react native·react.js·ecmascript·harmonyos