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
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;
}
};