I'm newbie with JavaScript and I tryed to store the Vector position and whith two buttons (forward and backward) to move the Camera to that specific position. Also I tried to use 'gsap' to have soft movements but the code don't work.
//Coordinates
let positionIndex = 0;
const positions = [
{
x: -0.05,
y: 0.6,
z: -0.17
},
{
x: -2,
y: 1,
z: 1
},
{
x: 0.3,
y: 0.6,
z: -0.6
}
]
//on buttonforward clicked
var element = document.querySelector(".button-1");
element.onclick = function MoveUp(){
if(positionIndex) == 3
{
positionIndex = 0;
}
else
{
positionIndex += 1;
}}
camera.position.x = positions[positionIndex].x
camera.position.y = positions[positionIndex].y
camera.position.z = positions[positionIndex].z
//on buttonbackwards clicked
var element = document.querySelector(".button-2");
element.onclick = function MoveDown(){
if(positionIndex) == 0
{
positionIndex = 3;
}
else
{
positionIndex -= 1;
}}
camera.position.x = positions[positionIndex].x
camera.position.y = positions[positionIndex].y
camera.position.z = positions[positionIndex].z
Someone can give me advice?
Thank you.
when you set positionIndex to 3 you will get undefined. Perhaps it would be better to use positions.length - 1