I am currently learning Three.js and attempting to create my first game: an endless runner.
After reading this article, I aim to create a similar game where the main character, a blue ball, rolls infinitely forward while dodging obstacles that appear in its path. Players can control the ball by moving it left or right and jumping using the keyboard (left/right arrow keys and space bar).
Here is my initial concept: https://i.sstatic.net/RNILd.jpg
While inspired by the article, I intend to understand and not simply copy the code. Here is what I have accomplished so far:
let sceneWidth = window.innerWidth;
let sceneHeight = window.innerHeight;
// Remaining JavaScript code...
<script src="https://threejs.org/build/three.min.js"></script>
(JSFiddle)
However, I am facing several issues, starting with object and camera positioning.
I would like the ground plane's shorter side aligned with the screen edge to ensure full visibility without any parts hidden off-screen. Additionally, I want the ball centered horizontally and positioned near the start of the floor vertically, as shown in the figure, with its shadow cast on the plane. I am using a spotlight and Lambert materials for shadows, but they are not displaying. Why?
I also need help with object placement. While my current code functions, I wonder if there are more efficient ways to handle positioning.
Lastly, I plan to adjust the sphere's radius from 0.03 to 1 and scale down the scene by zooming out the camera. This change will simplify the scene setup.
If anyone has advice or tips, especially regarding setting up the scene correctly, please share!
EDIT 1
I modified
camera.lookAt(new THREE.Vector3(0, 0, 0));
to camera.lookAt(new THREE.Vector3(0, 0, -5));
and added spotLight.lookAt(new THREE.Vector3(0, 0, -5));
.
The updated result can be viewed here: https://i.sstatic.net/qF0ns.png
This outcome is not exactly what I had in mind...