I
I
IliaNeverov2020-06-16 17:19:46
JavaScript
IliaNeverov, 2020-06-16 17:19:46

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

1 answer(s)
D
Dmitry, 2020-06-16
@IliaNeverov

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 question

Ask a Question

731 491 924 answers to any question