我通过chatgpt帮我写代码,他帮我写了这么一个代码
const direction = this.targetPosition.subtract(this.npcInstance.position).normalize();
嗯看起来是没什么问题。
direction 被修改了,其他值都是正常的。
但是这里有个隐藏的问题在3.8.1
我通过不停的debug。发现他实际运行的函数是
/**
* @en Subtracts one vector from this, and returns this.
* @zh 向量减法。将当前向量减去指定向量的结果。
* @param other specified vector
*/
public subtract (other: Vec3): Vec3 {
this.x -= other.x;
this.y -= other.y;
this.z -= other.z;
return this;
}
这里造成了一个结果this.targetPosition的值也被修改了。
所以正确的写法应该是
let newPosition = this.npcInstance.position.add(direction.multiplyScalar(moveStep));
我也不知道subtract为什么要这么写居然能改变数值本身。