[SOLVED] 2D Jumping Error

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/

After reviewing the project, it seems there are no errors.

Could you please provide more specific details on which part might be the issue?

2 Likes

Hi, Hizard! Thanks for replying. I can’t put it into words but here’s what it looks like when I try to play the game :frowning_face: :

  1. he doesn’t jump on the platform obstacle and walk on it, he is only blocked by it on the first encounter
  2. after jumping, his landing is not on the ground platform

Hi @yzhdcn!

Line 21, 28, 46 and 52 are probably the reason for your problems.

this.entity.setPosition(this.entity.getPosition().x, 3.51, this.entity.getPosition().z);

It seems like you use the same position for the y-axis without taking anything into account.

Maybe the code below can solve this, but I haven’t analyzed your logic enough to say this with certainty.

var pos = this.entity.getPosition();
this.entity.setPosition(pos.x, pos.y + 3.51, pos.z);
1 Like

Thank you, @Albertos ! :heartpulse:

Can I also ask for you to see my work? Starting from the “startup” scene and playing it until the end.

I’m not sure if it is only on my end that on the 2nd level ‘next level’ scene, my character cannot interact with other entities (stars and bees) when played from the ‘startup’ scene to the end. But if I launch the game from the ‘next level’ directly, everything works. Do you have any tips to resolve it?

I’m scared that if I publish it, it won’t work and I cannot change it again. Thank you so much!

Overview | Dashboard | DECENA_MMINTDS_Capydada_Finals | PlayCanvas | 3D HTML5 & WebGL Game Engine

Here’s a recording of the current game when launched from ‘startup’ to the ‘next level’.

But if I directly launch the 2nd level which is the ‘next level’ scene, everything works, even the star counter.

(I sped the video up)

I’m currently on mobile so I can’t test your project, but I already noticed some strange things. For example on line 42 of your collisionScript I see aa on the end of the line. I also suggest to try the code below to change your scenes and don’t use the other related code, because you can easily do something wrong.

this.app.scenes.changeScene('Some Scene Name');
1 Like

Really helped and now it works! Thank you so much for the effort and solution! :sob: :heartbeat:

1 Like