Hello,
I am trying to figure out how to move the universal camera with the default key commands, but nothing I do seems to work. The documentation says that the arrow keys will move the camera after camera.attachControl(canvas, true) has been called.
This is not happening in my scene though. The arrow keys don’t seem to be changing any values as I press them.
I also do not see any “move” method on the universal camera API. Is there a method that will do physics and collision detection from point A to point B?
Here is my code:
(Note that I’m passing the HTML canvas element into the exported function:
import { Engine } from "@babylonjs/core/Engines/engine";
import { Scene } from "@babylonjs/core/scene";
import { Vector3 } from "@babylonjs/core/Maths/math";
import { UniversalCamera } from "@babylonjs/core/Cameras/universalCamera";
import { Mesh } from "@babylonjs/core/Meshes/mesh";
// Required side effects to populate the Create methods on the mesh class. Without this, the bundle would be smaller but the createXXX methods from mesh would not be accessible.
import "@babylonjs/core/Meshes/meshBuilder";
import "@babylonjs/core/Materials"
export default function app(canvas){
const engine = new Engine(canvas)
var scene = new Scene(engine);
scene.gravity = new Vector3(0, -9.81, 0)
scene.collisionsEnabled = true
// This creates and positions a free camera (non-mesh)
var camera = new UniversalCamera("camera1", new Vector3(1, 1, 1), scene);
camera.checkCollisions = true
camera.applyGravity = true
camera.ellipsoid = new Vector3(1, 1, 1)
// This targets the camera to scene origin
camera.setTarget(Vector3.Zero());
// This attaches the camera to the canvas
camera.attachControl(canvas, true);
// Our built-in 'sphere' shape. Params: name, subdivs, size, scene
var sphere = Mesh.CreateSphere("sphere1", 16, 2, scene);
sphere.checkCollisions = true
// Move the sphere upward 1/2 its height
sphere.position.y = 2;
//when I press f, the camera position is printed to the console (this works, so I know canvas is getting events):
canvas.addEventListener("keydown", e=>{
if(e.key === "f") console.log(camera.position)
})
// Render every frame
engine.runRenderLoop(() => {
scene.render();
})
}