简体   繁体   中英

how to make loop for a cube going up and down in three js

This example code the box only moves up I want it move up and down

    BoxGeo = new HREE.BoxGeometry(10,10,10)
    BoxMat = new THREE.MeshPhongMaterial({
      color: 0xf3e54f
    }),
    cab = new THREE.Mesh ( BoxGeo, BoxMat);
    scene.add(cab);
   
cab.position.y  += 1;

Since Math.random() gives you numbers in the [0, 1] range, you can change this range to [-1, 1] with a simple equation:

let displacement = (Math.random() - 0.5) * 2.0;
cab.position.y += displacement;

您需要在动画循环中使用Math.sin()以便平滑地上下移动对象。

cab.position.y = Math.sin(elapsedTime) + 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM