Box player movement

Hello guys, im creating Tomb of the Mask type game (also new into playcanvas and javascript) and was wondering how the movement works.

Simply. I want my box to move in direction depended on the arrow until front side of the box reach the wall in front of him, also direction cant be changed until it reaches the wall.

1 Like

Hello @smokys, you can make an entity move in 2 different ways.
the first way is by translating the entity’s position ( i don’t recommend this because it ignores physics)


// goes five studs forward on the z axis
this.entity.translate(0,0,5);
// You could also do translate local which moves it in the direction with the orientation of the entity
// goes five studs forward where the z axis is pointing
this.entity.translateLocal(0,0,5);

You also have a rigid body, they are the most effective when trying to move using physics, in order to use a rigid body you need a collision component and a rigid body component. Here’s how to use the rigid body in a script.


// this will go five studs on the z-axis like normal but it doesn't ignore physics.
this.entity.rigidbody.applyForce(0,0,5);
// you could do jumping by doing, basically what this bottom half is doing is applying a force upwards to the body
this.entity.rigidbody.applyImpulse(0,5,0)

to find out more about rigidbodys go to :

Rigidbody API

Also happy halloween. :smile:

1 Like

Thank you so much @Fus_ion, applyForce is probably what i need,i still wanna know how to make the speed increasing over time with apply force… also i have tried to add collision and rigidbody components, tried your code and attach script to that object but it seem to be not working :confused: !

Debug Log is clean without error.

Funny part is that im looking on the example projects with applyForce and it works there, i have the same project and it doesnt work for me.

I also created new project specially for that, added collision, rigidbody to my Box player , attached the script and nothing happens :smiley:
Screenshot is with applyImpulse but I also tried applyForce
playerMovementPlayCanvas|641x500

Happy halloween :smiley:

1 Like

an easy way to make speed increase over time is by making a variable add on to itself + a number.


// Example
Myscript.prototype.initialize = function(){
// when the script loads it runs this line
this.speed = 1;
}

// this is runned every frame
Myscript.prototype.update = function(dt){
// checks if the key is currently pressed
  if(this.app.keyboard.isPressed(pc.KEY_UP)){
      this.speed = this.speed + 1;
      this.entity.rigidbody.applyForce(0,0,this.speed);
  }
}

1 Like

Yeah thats a nice one, unfortunatelly i cannot apply that one in to my project because i need wasPressed function instead of isPressed as i only want my box to go in given direction when the button is pressed once… and it stops when u collided with a wall and then you can chose another direction.

Okay i fixed it, now i need to know how to make my box stop when it collides with a wall because it is passing through the wall

EDIT: fixed, now i need to know how to lock rotation on object so my object will never rotate.

@smokys look for this under your rigidbody component :


Screenshot 2020-10-30 at 5.54.38 PM


Where it says angular factor make sure you set all of the values to zero to stop it from rotating.

Thank you it helped… Is something wrong in this code ? Because when i hit other rigidbody, console isnt showing i collided with rigidbody (“You hit the wall”)

@smokys Well yes at the top where you put the on event


// the event isnt supposed to be colisionend but instead collisionend just a grammer error thats all
this.entity.collision.on('collisionend', this.onCollisionEnd, this); 

also for the collision end function, it’s not result2 but instead it’s other.

1 Like

Oh yeah didnt see missing “l” :smiley: . Okay thank you so much, i think we can close this topic, i have more issues but thats for new topic .

1 Like