遇到一个很奇怪的问题,setLinearVelocity 在一定范围内会导致移动过慢

  • Creator 版本: 3.8.5
  • 重现方式:cocos 模拟器预览
  • 首个报错: 无报错
  • 重现概率: 100%

玩家坐标:Vec3 {x: 2.89070987701416, y: 0, z: 0.5005103349685669},
敌人坐标:Vec3 {x: 2.3076634407043457, y: 0, z: 0.4183853268623352},
敌人向玩家移动时,计算距离distance: 1.3466376142052159,
速度是:Vec3 {x: 0.9902251476110268, y: 0, z: 0.13947815971943472} ,

这个是输出的结果,实际setLinearVelocity的就是速度Vec3的值,
首先讲一下,这里的距离1.34不是第一个出现问题的坐标,大概是在0.5+
我的模型boxcollider,size X=0.18 ,Y=0.3,Z=0.18 center x = -0.05,y=0.15 ,z=0
这个模型比较小一点,材质是用的default材质 摩擦0.8
rigidBody 的Linear Factor 是1 0 1, 转向的没问题我就不多说
Group设置正确,Type是DYNAMIC

详细说问题:
距离远的时候,移动都正常,可以正常的移动和碰撞到玩家位置,在玩家移动后,这个距离会拉大,同时敌人靠近后会执行攻击动画(动画正常播放和结束,验证过,不是动画问题),不一定会在首次移动距离到0.5的时候出现移动速度过慢的情况(看起来好像没有在跑,上面给到的速度是直接按距离计算出来归一后,默认速度是1),在尝试几次拉开一部分距离后(敌人攻击时会停留不移动,攻击完成后会执行移动),我发现敌人没有移动(看起来没有移动,实际移动的距离太小,我观察0.58的距离时,会慢慢降到0.5以内,然后执行攻击)。这个距离在0.5+的时候就算了,问题不大,关键是玩家角色会跑,距离越来越远,但是这个敌人不会跟随,就算距离再远,也不会增加速度,一旦卡在0.5+的移动速度时,只有我靠近让他攻击,才会重置。 如上,我的距离是1.34已经很近了,但是我跑到5也是一样的移动速度。

wakeup和摩擦和加大线性速度试过多不行,速度*5 在距离过近时确实可以减少一部分时间,但是依旧看起来像没有移动

代码如下:
// 获取玩家和敌人的位置
const playerPosition = this.player.node.worldPosition;
const enemyPosition = this.node.worldPosition;
// 计算距离和方向
const distance = Vec3.distance(playerPosition, enemyPosition);
const direction = new Vec3();
Vec3.subtract(direction, playerPosition, enemyPosition);
direction.y = 0; // 保持Y轴不变
direction.normalize();

// 计算目标速度 speed 是1 代码就不复制了

    const velocity = new Vec3(
        direction.x * speed,
        direction.y * speed,
        direction.z * speed
    );

        this.rigidBody.setLinearVelocity(velocity);

补充一下,代码在0.5时会停止移动,并执行攻击
这个是移动到0.49时的距离:distance: 0.4992943122330246
Animation finished: hit
Animation finished: gujia|attack
distance: 0.5478253488195152
攻击后,距离变成0.54是因为击退效果,玩家位移了
把具体坐标补充:
distance: 0.4992943122330246
player Vec3 {x: 0.8293922543525696, y: 0, z: 0.22813041508197784}
enemy Vec3 {x: 0.35034123063087463, y: 0, z: 0.08740043640136719}
direction: Vec3 {x: 0.9594561994795004, y: 0, z: 0.28185776781978705}
攻击后
distance: 0.5478253488195152
direction: Vec3 {x: 0.9594635678821473, y: 0, z: 0.2818326842416618}
player Vec3 {x: 0.8759596943855286, y: 0, z: 0.24179552495479584}
enemy Vec3 {x: 0.35034123063087463, y: 0, z: 0.08740043640136719}

然后我把角色移动的远一点,最后3帧数据如下:
distance: 3.0769857431032164
direction: Vec3 {x: 0.9797901652124619, y: 0, z: 0.20002807841134845}
player Vec3 {x: 3.409152030944824, y: 0, z: 0.7132962942123413}
enemy Vec3 {x: 0.39435166120529175, y: 0, z: 0.09781274944543839}

distance: 3.076884880028052
direction: Vec3 {x: 0.9797901652605746, y: 0, z: 0.20002807817567997}
player Vec3 {x: 3.409152030944824, y: 0, z: 0.7132962942123413}
enemy Vec3 {x: 0.39445048570632935, y: 0, z: 0.09783292561769485}

distance: 3.076784016952887
direction: Vec3 {x: 0.9797901653086903, y: 0, z: 0.200028077939996}
player Vec3 {x: 3.409152030944824, y: 0, z: 0.7132962942123413}
enemy Vec3 {x: 0.39454931020736694, y: 0, z: 0.09785310178995132}

可见是有移动的,不过移动速度太慢了
重现的方法是,在攻击动画时,把玩家的距离拉开超过0.5,这样敌人就会执行移动
数据如下:
distance: 0.0764768881970112
Actor.ts:158 direction: Vec3 {x: 0.9996112297751805, y: 0, z: -0.027881702016763824}
Actor.ts:159 player Vec3 {x: 0.3432691693305969, y: 0, z: 0.1772744208574295}
Actor.ts:160 enemy Vec3 {x: 0.31935134530067444, y: 0, z: 0.2499149888753891}

Animation finished: hit
Animation finished: gujia|attack

Actor.ts:157 distance: 0.5142606812063285
Actor.ts:158 direction: Vec3 {x: -0.9584623206706561, y: 0, z: -0.285219178623424}
Actor.ts:159 player Vec3 {x: -0.17432712018489838, y: 0, z: 0.10588158667087555}
Actor.ts:160 enemy Vec3 {x: 0.31935134530067444, y: 0, z: 0.2499149888753891}

Actor.ts:157 distance: 0.5275970055566218
Actor.ts:158 direction: Vec3 {x: -0.9599770768543399, y: 0, z: -0.28007858167699456}
Actor.ts:159 player Vec3 {x: -0.18787625432014465, y: 0, z: 0.10472971946001053}
Actor.ts:160 enemy Vec3 {x: 0.31935134530067444, y: 0, z: 0.2499149888753891}

Actor.ts:157 distance: 0.540893742972466
Actor.ts:158 direction: Vec3 {x: -0.9613921123105832, y: 0, z: -0.27518213311731404}
Actor.ts:159 player Vec3 {x: -0.20142081379890442, y: 0, z: 0.1035819947719574}
Actor.ts:160 enemy Vec3 {x: 0.3193061351776123, y: 0, z: 0.24990180134773254}