Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make it that a vector in an array doesn't change its values?

When I insert a vector in an array and change the value of the vector, it also changes in the array. How can I safe data in an array in type of a vector without it changing the value when I change the value of the vector?

const array10 = [];
let vector = new THREE.Vector3(0,0,0);
array10.push(vector);
vector.x = 10;
console.log(array10[1]);

Output: (10,0,0)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Put a copy of the vector in the array. A new vector3 with the same x, y, and z values can be created with clone():

array10.push(vector.clone());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading