[SOLVED] A and D error Mario Game

We were assigned to make a game similar to a Mario game.
When I worked on collision objects, W and S works fine for up and down but my character doesn’t move from left to right when I click A and D.

Hi @marymarzx and welcome,

Can you share some of your movement code and maybe your player controller entity setup?

This is the code

var CharacterMovementControls = pc.createScript('characterMovementControls');


var movementSpeed = 15;

// initialize code called once per entity
CharacterMovementControls.prototype.initialize = function() {
//changing translateLocal to this.entity.rigidbody.applyForce     
};

// update code called every frame
CharacterMovementControls.prototype.update = function(dt) {
   if (this.app.keyboard.isPressed(pc.KEY_W)) {
    this.entity.sprite.play("WalkingUp") ;
    this.entity.rigidbody.applyForce(0,movementSpeed,0);
}
    
    if (this.app.keyboard.isPressed(pc.KEY_S)) {
    this.entity.sprite.play("WalkingDown") ;
    this.entity.rigidbody.applyForce(0,-movementSpeed,0);
}
    
    if (this.app.keyboard.isPressed(pc.KEY_A)) {
    this.entity.sprite.play("WalkingLeft") ;
    this.entity.rigidbody.applyForce(-movementSpeed,0,0);
}
    
    if (this.app.keyboard.isPressed(pc.KEY_D)) {
    this.entity.sprite.play("WalkingRight") ;
    this.entity.rigidbody.applyForce(movementSpeed,0,0);
}

   
};

// swap method called for script hot-reloading
// inherit your script state here
// CharacterMovementControls.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

I think everything is correct, but I am thinking if it’s a 2D game maybe you have everything oriented on the YZ plane?

So maybe try changing the axis on which you apply the left/right movement force:

    if (this.app.keyboard.isPressed(pc.KEY_A)) {
    this.entity.sprite.play("WalkingLeft") ;
    this.entity.rigidbody.applyForce(0,0,-movementSpeed);
}
    
    if (this.app.keyboard.isPressed(pc.KEY_D)) {
    this.entity.sprite.play("WalkingRight") ;
    this.entity.rigidbody.applyForce(0,0,movementSpeed);
}
1 Like

yes that’s right hahahahaa thank you so much!

1 Like