Platforming Physics

So I’m using the 3rd person controller for the base of this project, however, there is an issue with the physics where the player will slide up a platform (things with collision) instead of just falling. Could someone help me with this?

Project: https://playcanvas.com/project/790841/overview/game-2 (Main 3)

Hi @Overbaked_Prod,

That happens due to friction between the Player and the scene bodies. You can try lowering it or even setting it to zero to get rid of that behavior.

1 Like

How would I go about doing that? (Sorry for all the questions, I am fairly new to PlayCanvas and I’ve been trying to learn as much as I can by myself but you see how that’s turning out.)

So find your player entity and to the right in the inspector panel go to the rigid body component and set the friction slider to zero.

Of required do the same for your scene static bodies.

Ok thank you, I’m in a group and I’ve been doing most of the heavy work so I really appreciate the help.

I do have a question, I’ve made a script for a sliding command in the game and I’m wondering how I’d be able to “shrink” down the collision box for the player as well as keep the slide animation from repeating.

Animation Script

var PlayerAnimationHandler = pc.createScript('playerAnimationHandler');

PlayerAnimationHandler.attributes.add('blendTime', { type: 'number', default: 0.4 });

PlayerAnimationHandler.prototype.initialize = function () {
    var app = this.app;
    app.keyboard.on(pc.EVENT_KEYDOWN, this.keyChange, this);
    app.keyboard.on(pc.EVENT_KEYUP, this.keyChange, this);

    this.direction = 'Idle';
    this.setDirection('Idle');
};

PlayerAnimationHandler.prototype.checkButtons = function () {
    var app = this.app;
    
    var w = app.keyboard.isPressed(pc.KEY_W);
    var a = app.keyboard.isPressed(pc.KEY_A);
    var s = app.keyboard.isPressed(pc.KEY_S);
    var d = app.keyboard.isPressed(pc.KEY_D);
    var space = app.keyboard.isPressed(pc.KEY_SPACE);
    var slide = app.keyboard.isPressed(pc.KEY_SHIFT);
    var crouch = app.keyboard.wasPressed(pc.KEY_SHIFT);
    var idle_crouch = app.keyboard.isPressed(pc.KEY_SHIFT);
    var boost = app.keyboard.isPressed(pc.KEY_R);
    var unboost = app.keyboard.wasReleased(pc.KEY_R);
    var stand = app.keyboard.wasReleased(pc.KEY_SHIFT);

    if (w && !s) {
        if (space) {
            this.entity.animation.loop = false;
            this.direction = 'Jump';
        } else if (slide){
            this.direction = 'Slide';
        } else if (boost){
              this.direction = 'Sprint';
            this.entity.particlesystem.play();
            
        } else if (a && !d) {
            this.direction = 'Run Forward Left';
        } else if (d && !a) {
            this.direction = 'Run Forward Right';
        } else {
            this.entity.animation.loop = true;
            this.direction = 'Run Forward';
            
        }
    } else if (s && !w) {
        if (a && !d) {
            this.direction = 'Run Backward Left';
        } else if (d && !a) {
            this.direction = 'Run Backward Right';
        } else {
            this.direction = 'Run Backward';
            if (space) {
                this.direction = 'Jump Backward';
            }
        }
    } else if (a && !d) {
        this.direction = 'Run Left';
        
    } else if (d && !a) {
        this.direction = 'Run Right';
     
    } else if (space) {
        this.entity.animation.loop = false;
        this.direction = 'Jump';
        
         } else if (stand){
         this.entity.animation.loop = false;
            this.direction = 'Stand';
    
    }else if (crouch){
        this.entity.animation.loop = false;
        this.direction = 'Crouch';
     
          }else if (unboost){
        this.entity.particlesystem.stop();
        this.entity.animation.loop = false;
        this.direction = 'Stop';
        
    }else if (idle_crouch){
        this.entity.animation.loop = true;
        this. direction = 'Crouch Idle';
     
    } else {
        this.entity.animation.loop = true;
        this.direction = 'Idle';
    }  

};

PlayerAnimationHandler.prototype.keyChange = function (e) {
    var tempDirection = this.direction;

    this.checkButtons();

    if (tempDirection !== this.direction) {
        this.setDirection(this.direction);
    }
};

PlayerAnimationHandler.prototype.setDirection = function (direction) {
    this.direction = direction;
    this.entity.animation.play(direction, this.blendTime);
};

Player Movement Script:

var PlayerMovement = pc.createScript('playerMovement');

PlayerMovement.attributes.add('speed', { type: 'number', default: 0.09 });
PlayerMovement.attributes.add('boost', { type: 'number', default: 1 });

PlayerMovement.prototype.initialize = function () {
    var app = this.app;
    var camera = app.root.findByName('Camera');
    this.cameraScript = camera.script.cameraMovement;
    app.keyboard.on(pc.EVENT_KEYDOWN, this.keyChange, this);
    app.keyboard.on(pc.EVENT_KEYUP, this.keyChange, this);
};

var origSpeed = this.speed;
var speedBoost = false;

PlayerMovement.prototype.update = function (dt) {
    var app = this.app;

    var forward = this.entity.forward;
    var right = this.entity.right;

    var x = 0;
    var z = 0; 

    if (app.keyboard.isPressed(pc.KEY_A)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S)) {
        x -= forward.x;
        z -= forward.z;
    }
    
    if (app.keyboard.isPressed(pc.KEY_R)) {
        if (speedBoost === false) {
            origSpeed = this.speed;
            this.speed = this.speed * this.boost;
            speedBoost = true;
        }
    }
    if (app.keyboard.wasReleased(pc.KEY_R)) {
        this.speed = origSpeed;
        speedBoost = false;
    }
 
    if (app.keyboard.isPressed(pc.KEY_SHIFT)) {
        

    if (x !== 0 || z !== 0) {
        var pos = new pc.Vec3(x * dt, 0, z * dt);
        pos.normalize().scale(this.speed);
        pos.add(this.entity.getPosition());

        var targetY = this.cameraScript.eulers.x + 180;
        var rot = new pc.Vec3(0, targetY, 0);

        this.entity.rigidbody.teleport(pos, rot);
    }
}
};

This proof of concept was made using the third person controller with new and modified scripts. (I do want to release it as a new project people can fork for Third Person Platformers.)

Project Link: https://playcanvas.com/project/790841/overview/game-2 (Main 3)