Answer the question
In order to leave comments, you need to log in
How to make the cube, when a certain threshold is reached, return to where it started from?
Hello I have a cube and it moves on the X axis with this function:
var ypos = 1;
var render = function () {
requestAnimationFrame(render);
cube.position.x += ypos;
renderer.render(scene, camera);
};
Please tell me how to make it so that when a certain threshold is reached, it returns to where it started from.
Answer the question
In order to leave comments, you need to log in
var ypos = 1, minPos = 0, maxPos = 5000;
var render = function () {
requestAnimationFrame(render);
const newPosition = cube.position.x + ypos;
if (newPosition > maxPos || newPosition < ypos)
ypos = -ypos;
else
cube.position.x = newPosition
renderer.render(scene, camera);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question