Disable the gravity dynamically?

Does anyone have any ideas on how would one go about having an universal camera which applyGravity = true, but then as the camera goes inside a box, have the camera fly?

I’ve played with it but I still can’t figure it out. Any advice?

Well if you detect that the camera is in the box, setting camera.applyGravity = false should do it

Can you share a repro in the PG if this is not working?

Do you know what function to use for detecting if camera is in the box?

if (camera.intersectsMesh(box, true)) {
camera.applyGravity = false;}

Doesn’t work. Am I missing something?

Your camera is like a dot in space so a simple point vs box should do it:

box.getBoundingInfo().boundingBox.intersectsPoint(camera.position)

I’ve this but I simply can’t figure out how to do it.

camera.onCollide = function (colMesh) {
		if (box.getBoundingInfo().boundingBox.intersectsPoint(camera.position)) {
            camera.applyGravity = false;
        } 
	}

https://playground.babylonjs.com/#8Z0MKW

not oncollide but on scene.onBeforeRenderObservable as you need to test it on each frame

scene.onBeforeRenderObservable.add(()=>{
		if (box.getBoundingInfo().boundingBox.intersectsPoint(camera.position)) {
            camera.applyGravity = false;
        } 
	});

Still nothing…
https://playground.babylonjs.com/#8Z0MKW#1

your PG works :slight_smile: the gravity is off

I am trying to have gravity off only within the box - like flying, and when the camera is outside, to have gravity on - like walking

well then change your code to restore the gravity when the condition is wrong :wink:

1 Like
scene.onBeforeRenderObservable.add(()=>{
		if (box.getBoundingInfo().boundingBox.intersectsPoint(camera.position)) {
            camera.applyGravity = false;
        } else {camera.applyGravity = true;}
	});

Now it works. You’re awesome.

1 Like