[英]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:由于 Math.random() 为您提供[0, 1]
范围内的数字,您可以使用一个简单的等式将此范围更改为[-1, 1]
:
let displacement = (Math.random() - 0.5) * 2.0;
cab.position.y += displacement;
您需要在动画循环中使用Math.sin()
以便平滑地上下移动对象。
cab.position.y = Math.sin(elapsedTime) + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.