The position value in the variable changes

This is Playground that reproduces the situation I experienced.

The Position value of the Sphere that you put in the variable is updated in real time. Do you know why?

My goal is to save the initial position of the sphere and change the position of the sphere to the initial value at the desired time.

when doing

spherePosition.push(sphere.position);

The reference to the position is pushed in the array, not the value itself. If you want to push the value, you need to do a copy.

spherePosition.push(sphere.position.clone());

Push mesh.position is quate,you can clone it or new Vector3 then copyFrom(mesh.position).

When the clone is applied, it is operated only once. The second is the same phenomenon as before…

it’s the same issue. you set a reference, not a value.
change line 51 to be:

sphere.position = spherePosition[0].clone();

Wow! Thank you so muchHahaha
Can you tell me why?

I did an experiment! But I found a solution! Thank you for your answer.Haha

Because the position is object and then every change of the object will take effect.So the mesh position will change.

Like this:
var obj = {
a: 1
};
var obj1 = obj;
obj.a = 2;
console.log(obj1.a) // console 2
image

1 Like

Aha! I seeHaha
Thank you for your help!! :grinning: :grinning: