2d left and right movement

I need help figuring out how to move my character(player) from left to right, and vice versa

here is the script my player entity has

var Player = pc.createScript('player');

// initialize code called once per entity
Player.prototype.initialize = function() {

};

// update code called every frame
Player.prototype.update = function(dt) {
    var posX = 0;
    if(this.app.keyboard.wasPressed(pc.KEY_LEFT)){
        posX -= 5;
    }else if(this.app.keyboard.wasPressed(pc.KEY_RIGHT)){
        posX += 5;
    }

    this.entity.setPosition(posX,0,0);
};

Hi @patrick_ramos and welcome!

Maybe you can try to replace line 10 with the code below.

var posX = this.entity.getPosition().x;


I have rewritten the script but I encountered another problem.
how to fix the delay on walk(updating x)
and update the position of my player after jumping
here’s the script on my player

var Player = pc.createScript('player');

// initialize code called once per entity
Player.prototype.initialize = function() {
    this.jumpX = 2;
    this.jumpY = 10;

    

    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
    this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
};

// update code called every frame
Player.prototype.update = function(dt) {
    
};

Player.prototype.onKeyDown = function() {
    const posX = this.entity.getPosition();
    const posY = this.entity.getPosition();
    
    if (this.app.keyboard.isPressed(pc.KEY_SPACE)){
        
        this.jump();
        
    }
    else if (this.app.keyboard.isPressed(pc.KEY_D)){
        this.entity.sprite.play("walk");
        this.entity.setPosition(posX.x + 0.03,posY.y,0);
    }
    else if (this.app.keyboard.isPressed(pc.KEY_A)){
        this.entity.sprite.play("walk-left");
        this.entity.setPosition(posX.x + -0.03,posY.y,0);
    }
};

Player.prototype.onKeyUp = function(e) {
     if(e.key === pc.KEY_D){
         this.entity.sprite.play("idle");
     }else if(e.key === pc.KEY_A){
         this.entity.sprite.play("idle-left");
     }
};

Player.prototype.jump = function(){
    
    this.entity.rigidbody.linearVelocity = new pc.Vec3(this.jumpX,this.jumpY,0);
    this.entity.sprite.play("jump");
    const posX = this.entity.getPosition();
    const posY = this.entity.getPosition();
    this.entity.setPosition(posX.x,posY.y,0);
};

I’m not sure what can cause the delay if I look at your code.

I also see on your video that the jump doesn’t start at the correct position.

What is the type of rigidbody you use for the player entity?

It has dynamic
here’s my project
https://playcanvas.com/editor/scene/1906109

Unfortunately, you cannot move an entity with a dynamic rigidbody using setPosition(), translate() or translateLocal(). The entity is moved, but the rigidbody is not. This is most likely the cause of the problems in your project. The easiest solution is to change your rigidbody to kinematic, but that means it won’t stop for obstacles. If this is not what you want, you have to change the way of movement. Possible ways to change the position of an entity with a dynamic rigidbody is to move the rigidbody using for example applyForce(), teleport() or changing the linearVeloctiy, like you do for jumping.

1 Like

I recommand teleport

like this:

this.entity.rigidbody.teleport(this.entity.getPosition().x + 0.03 , this.entity.getPosition().y, 0);

this.entity.rigidbody.teleport(this.entity.getPosition().x - 0.03 , this.entity.getPosition().y, 0);

2 Likes