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;
}