Canceling gravity on a specific object

I wanted to make a moving platform that my character to stand on however I don’t know is it because of how I wrote my code but it doesn’t seem to be working. I want the platform to not be affected by gravity but to also be movable

platforms = this.physics.add.sprite(370, 430, ‘platform’).setIgnoreGravity(true);
whenever I execute this code the whole game crashes

I’ve also tried
this.physics.add.sprite(370, 430, ‘platform’).body.allowGravity = false;

which simply just makes my character phase through the platform

Hi dubler,

You nearly had it! :slight_smile:

Use setAllowGravity(false); to disable gravity on the platform.
platforms = this.physics.add.sprite(370, 430, ‘platform’).setAllowGravity(false);

Hope this helps :slight_smile:

2 Likes

I played around with it a little and nothing seemed to work everything underneath that line of code seemed to stop functioning

platforms = this.physics.add.sprite(370, 430, ‘platform’).setAllowGravity(false);

I just found on this other website that a combination of this code works you have to write them both out however.

platforms = this.physics.add.image(370, 430, ‘platform’).setImmovable(true)

and

platforms.body.setAllowGravity(false);

makes the platform unaffected by gravity and an object that you can add later code to make it move

1 Like

Yep you are you right, sorry I forgot to mention you need to add setImmovable(true)
I’m glad it’s now working for you :slight_smile: