In my Capydada game, it is a 2D Scroller game with 2 levels. Players can collect stars and jump on platforms. It can’t jump on platforms. And the camera follows the character but it kind of glitches. Is there any way I could get help? Thank you!
Here’s the link to the scene: [draft] Capydada Final Game | Editor (playcanvas.com)
Here’s the code I have right now!
var MovementWithJump = pc.createScript('movementWithJump');
var onGround = true;
var isMoving = false;
var timer = 0;
// initialize code called once per entity
MovementWithJump.prototype.initialize = function() {
this.entity.setPosition(-9.53, 3.51, -1.75); // Set initial position
this.idleAnimation = "idle";
};
// update code called every frame
MovementWithJump.prototype.update = function(dt) {
if (!this.app.keyboard.isPressed(pc.KEY_D) &&
!this.app.keyboard.isPressed(pc.KEY_A) &&
!this.app.keyboard.isPressed(pc.KEY_W) &&
!this.app.keyboard.isPressed(pc.KEY_S)) {
// No keys pressed, play idle animation
this.entity.sprite.play(this.idleAnimation);
this.entity.setPosition(this.entity.getPosition().x, 3.51, this.entity.getPosition().z); // Set Y position
return; // Skip the rest of the update function
}
if (this.app.keyboard.isPressed(pc.KEY_W) && onGround) {
onGround = false;
this.entity.sprite.play("up");
this.entity.setPosition(this.entity.getPosition().x, 3.51, this.entity.getPosition().z); // Set Y position
}
if (!onGround) {
timer += dt;
if (timer < 0.2) {
this.entity.translateLocal(0, 0.50, 0);
} else if (timer < 0.1) {
this.entity.translateLocal(0, -0.20, 0);
} else {
onGround = true;
timer = 0;
}
}
if (this.app.keyboard.isPressed(pc.KEY_A)) {
this.entity.sprite.play("left");
this.entity.translateLocal(-0.1, 0, 0);
this.entity.setPosition(this.entity.getPosition().x, 3.51, this.entity.getPosition().z); // Set Y position
}
if (this.app.keyboard.isPressed(pc.KEY_D)) {
this.entity.sprite.play("right");
this.entity.translateLocal(0.1, 0, 0);
this.entity.setPosition(this.entity.getPosition().x, 3.51, this.entity.getPosition().z); // Set Y position
}
};
// swap method called for script hot-reloading
// inherit your script state here
// MovementWithJump.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/