How To Create Water Physics?

I’m trying to create some water physics in a 2D game meaning I want the player to move & fall slowly, but my code isn’t working:

var PlayerController = pc.createScript('playerController');

//Attributes
PlayerController.attributes.add("xspeed", { type: 'number', title: 'X Speed' });
PlayerController.attributes.add("yspeed", { type: 'number', title: 'Y Speed' });
PlayerController.attributes.add("water", { type: 'bool', title: 'Touching Water' });

// initialize code called once per entity
PlayerController.prototype.initialize = function() {
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);    
};

// update code called every frame
PlayerController.prototype.update = function(dt) {
    this.move(dt);
    this.entity.collision.on('contact', this.onCollide, this);
};

PlayerController.prototype.move = function(dt) {
    var Dt = dt;
    
   // if (this.water === false) {
        if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
            this.entity.rigidbody.applyForce(this.xspeed * dt, 0, 0);
        }
        if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
            this.entity.rigidbody.applyForce(-this.xspeed * dt, 0, 0);
        }
        if (this.app.keyboard.wasPressed(pc.KEY_UP)) {
            this.entity.rigidbody.applyImpulse(0, this.yspeed * dt, 0);
        //}
    }
    /*if (this.water === true) {
        if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
            this.entity.rigidbody.applyForce((this.xspeed/2) * dt, 0, 0);
        }
        if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
            this.entity.rigidbody.applyForce((-this.xspeed/2) * dt, 0, 0);
        }
        if (this.app.keyboard.wasPressed(pc.KEY_UP)) {
            this.entity.rigidbody.applyImpulse(0, (this.yspeed/2) * dt, 0);
        }
    }*/
};

PlayerController.prototype.onCollide = function(result) {
    if (result.other.tags.has('water')) {
        
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// PlayerController.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Link to Editor: https://playcanvas.com/editor/scene/803931

1 Like

Ill look into it after school Im on my chromebook rn,I’ll check it out when I get on my pc

Same lol

1 Like