Hi, I´m trying to move a object from left to right using lerp (like unity) but is not working as I expected:
update(): void {
const wp = getWorldPosition(this.gameObject);
this.gameObject.position.lerpVectors(this.gameObject.position,new Vector3(wp.x,wp.y,wp.z+5),1 )
this.gameObject.position.lerpVectors(this.gameObject.position,new Vector3(wp.x,wp.y,wp.z-5),1 )
console.log(this.gameObject.position);
}
Original Post on Discord
by user 632418299711324161
kipash
(kipash)
2
Hey
in this case you can use .lerp
istead of .lerpVectors
, that can reduce the length of the code.
https://threejs.org/docs/index.html#api/en/math/Vector3.lerp
Mind that you are setting world position to your local position vector. Can you make sure that the values are correct?
If you’re always lerping with “1” as second parameter your object will jump there immediately.
You’ll most likely want something like this:
private targetPos = new Vector3(0,0,0);
onEnable() {
getWorldPosition(this.gameObject, targetPos);
targetPos.z -= 5;
}
update() {
const wp = getWorldPosition(this.gameObject);
wp.lerp(targetPos, this.context.time.deltaTime * 2);
setWorldPosition(this.gameObject, wp);
}
getWorldPosition / setWorldPosition are a bit more convoluted than in Unity but otherwise it’s the same 
Thank you 
by user 632418299711324161