[SOLVED] My Character jumps back to original position when I applied rigidbody.applyForce

Hello, I’m new to PlayCanvas and my prof made us do a character moving then colliding in a wall. He said to apply rigid body and collision on both the character and the wall, so I did. He also asked us to change the script “this.entity.translateLocal” to “this.entity.rigidbody.applyForce”. I tried doing it first in one key ( I have 2 keys for movement the arrow keys and WASD keys) but then whenever I pressed that key it jumped like it glitched and went back to the original position. Also whenever we typed in the script, there should be suggestions appearing but I don’t have that feature on me.

When I first applied the rigidbody and collision (before doing the script) to my character, the left and right button just suddenly changed hence the picture.
Also here is the link: PlayCanvas | HTML5 Game Engine

Hi @Maristotle and welcome! I took a look at your project. The glitch that your character is jumping back to the orginal position is probably because you use translateLocal first, the rigidbody is not moving with it when you use a dynamic rigidbody. Also a force of 0.1 seems very low to me. Try to increase this a little. I also learned that the linear damping of a dynamic rigidbody component should be 0.99, don’t ask me why.

Thank you for the reply, I changed the linear damping to 0.99 and also changed all the translate.Local to rigidbody.applyForce. But it seems like my character is not moving at all?

CharacterMovementControls.prototype.initialize = function() {
      
};

// update code called every frame
CharacterMovementControls.prototype.update = function(dt) {
    
//  if I press W    
    if (this.app.keyboard.isPressed(pc.KEY_W)) {
        
    //  character walks up
        this.entity.sprite.play("WalkingUp");
         this.entity.rigidbody.applyForce(0,movementSpeed,0);
    }
    
//  if I press arrow up
   
    if (this.app.keyboard.isPressed(pc.KEY_UP)){
        
    //  character walks up
        this.entity.sprite.play("WalkingUp");
         this.entity.rigidbody.applyForce(0,movementSpeed,0);
    }

    
//  if I press S 
   if (this.app.keyboard.isPressed(pc.KEY_S)) {
  
    //  character walk down 
       this.entity.sprite.play("WalkingDown"); 
        this.entity.rigidbody.applyForce(0,-movementSpeed,0);
    }
    
//  if I press arrow down
     if (this.app.keyboard.isPressed(pc.KEY_DOWN)){
         
    //  character walk down 
        this.entity.sprite.play("WalkingDown");
         this.entity.rigidbody.applyForce(0,-movementSpeed,0);
    }

        
    
//  if I press D (THIS IS RIGHT)
     if (this.app.keyboard.isPressed(pc.KEY_D)) {
  
    //  character walk left 
       this.entity.sprite.play("WalkingLeft"); 
           this.entity.rigidbody.applyForce(-movementSpeed,0, 0);
    }
    
//  if I press arrow left
     if (this.app.keyboard.isPressed(pc.KEY_RIGHT)){
         
    //  character walk left 
        this.entity.sprite.play("WalkingLeft");
           this.entity.rigidbody.applyForce(-movementSpeed,0, 0);
         
    }
   
    
    
//  if I press A (THIS IS LEFT)
     if (this.app.keyboard.isPressed(pc.KEY_A)) {
  
    //  character walk right
       this.entity.sprite.play("WalkingRight"); 
          this.entity.rigidbody.applyForce(movementSpeed,0, 0);
         
    }
    
//  if I press arrow right
     if (this.app.keyboard.isPressed(pc.KEY_LEFT)){
         
    //  character right
        this.entity.sprite.play("WalkingRight");
         this.entity.rigidbody.applyForce(movementSpeed,0, 0);
    }
    
   
};

Did you also try to increase the movementSpeed?

1 Like

I tried! Holy molly it moved! Thank you very much! :sob: :sob: :sob: :sob: :heart: :heart: :heart: :heart:

Great! It looks very cute. :smiling_face_with_three_hearts:

1 Like

thank you

1 Like