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;
    }
相关推荐
bai2736 小时前
同一条 q=a+b:URLSearchParams 读成空格、decodeURIComponent 读成加号,11 条查询串打进裸 socket 的实测
javascript·后端
tryCbest7 小时前
Python 爬虫扩展篇(一):APP 抓包与 JS 逆向入门——从看见流量到读懂签名
javascript·爬虫·python
狮子座的男孩7 小时前
javascript 基础 es6:4、Symbol,Symbol创建对象属性,Symbol内置属性,迭代器,迭代器自定义遍历对象
javascript·经验分享·迭代器·es6·symbol·symbol创建对象属性·symbol内置属性
狮子座的男孩7 小时前
javascript 基础 es6:3、参数默认值,reset参数,spread扩展运算符,扩展运算符应用
前端·javascript·经验分享·es6·参数默认值·reset参数·spread扩展运算符
map_3d_vis7 小时前
JSAPI 4.0 的命名空间:BMap 和 BMapGL 为什么都能用
前端·javascript·typescript·百度地图
code2cat8 小时前
Vue项目实战之任务保存:编辑失败后,怎样保留输入再重试
前端·javascript·vue.js
1-1=08 小时前
Vue TodoList
前端·javascript·vue.js
多加点辣也没关系17 小时前
JavaScript|第41章:性能优化实战
开发语言·javascript·性能优化
feixing_fx19 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
小小小小宇1 天前
Chrome 扩展如何操作浏览器:chrome.debugger 与 CDP 的区别
javascript