[SOLVED] Grounded check won’t work

Hello there,

I am new to playcanvas so this is what i came up with my unity experience.

I am trying to get an grounded check but this wont work anny one advice on what methode to use normal i would just check if the player isgrounded or an ontrigger enter or maybe an small raycast

I am verry new to java and trying to learn it so if you might got some good tuturial dont mind linking them :stuck_out_tongue:

var Movement = pc.createScript('movement');
Movement.attributes.add('speed', {
    type: 'number',
    default: 0.25,
    description: 'movement speed'
});
Movement.attributes.add('jumpPower', {
    type: 'number',
    default: 0.25,
    description: 'high of the jump'
});
Movement.attributes.add('isGrounded', {
    type: 'boolean',
    default: true,
    description: 'if true = grounded if false = aired'
});
Movement.prototype.initialize = function() {        
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
Movement.prototype.onCollisionStart = function(collision) {
    if(collision.other.rigidbody && collision.other.tags.has('Ground')) {
        if(collision.other.getPosition().y <= (this.entity.getPosition().y - this.entity.collision.halfExtents.y)) {
            this.isGrounded = true;
        } else {
            this.isGrounded = false;
        }
    }
};
Movement.prototype.update = function(dt) {
    if(this.app.keyboard.isPressed(pc.KEY_D)||(this.app.keyboard.isPressed(pc.KEY_RIGHT))){
        this.entity.rigidbody.applyForce(this.speed,0,0);
    }
        if(this.app.keyboard.isPressed(pc.KEY_A)||(this.app.keyboard.isPressed(pc.KEY_LEFT))){
        this.entity.rigidbody.applyForce(-this.speed,0,0);
    }

            if(this.isGrounded&&this.app.keyboard.isPressed(pc.KEY_SPACE)||(this.isGrounded&&this.app.keyboard.isPressed(pc.KEY_W))||(this.app.keyboard.isPressed(pc.KEY_UP))){
                this.entity.rigidbody.applyForce(0,this.jumpPower,0);
                this.isGrounded=false;
            }
};

Hi @Timme_Kingma and welcome!

Both methods can also be used in PlayCanvas.

Can you share a project link so we can see the current setup and result?

hey thanks

https://playcanvas.com/project/866039/overview/testing

here you go

Thanks! What is not working as expected?

in the movement script the grounded check
i can infinit jump

As far I can see your this.isGrounded boolean is only set in onCollisionStart. That means it’s never set to false at the moment. Maybe you can try to set this.isGrounded to false when the player jumps.

I see in your script above you actually do it already, but you removed that in your project?

it works now ty :+1:

1 Like