When I use Camera Collisions to do collision checking, everything works fine. When I wanted to simulate jumping by clicking on space, I ran into some problems.
When I click the space once, it goes back to the original point nicely. When I click on the space multiple times in a row, it stays in mid-air. What’s the reason for this?
export class FirstPersonController {
scene: Scene
engine: Engine
constructor(private canvas: HTMLCanvasElement) {
this.engine = new Engine(this.canvas, true)
this.scene = this.CreateScene()
this.CreateEnvironment()
this.CreateController()
this.engine.runRenderLoop(() => {
this.scene.render()
})
}
CreateScene(): Scene {
const scene = new Scene(this.engine)
new HemisphericLight('hemi', new Vector3(0, 1, 0), this.scene)
scene.onPointerDown = (evt) => {
if (evt.button === 0) this.engine.enterPointerlock()
if (evt.button === 2) this.engine.exitPointerlock()
}
const framesPerSecond = 60
const gravity = -9.81
scene.gravity = new Vector3(0, gravity / framesPerSecond, 0)
scene.collisionsEnabled = true
return scene
}
async CreateEnvironment(): Promise<void> {
const { meshes } = await SceneLoader.ImportMeshAsync('', '/models/', 'env.babylon', this.scene)
meshes.map((mesh) => {
mesh.checkCollisions = true
})
}
CreateController(): void {
const camera = new FreeCamera('camera', new Vector3(0, 10, 0), this.scene)
camera.attachControl()
camera.applyGravity = true
camera.checkCollisions = true
camera.ellipsoid = new Vector3(1,1,1)
camera.minZ = 0.45
camera.speed = 0.75
camera.angularSensibility = 4000
this.scene.onKeyboardObservable.add(kbInfo => {
if(kbInfo.event.code === 'Space' && kbInfo.type === 1) {
camera.cameraDirection.y +=1
}
})
}
}
This is Playground:
camera collisions | Babylon.js Playground (babylonjs.com)
When I continuously click on the space, it does not land. Do I need to set anything extra?