Move an entity relative to another entity

Hi there, I have a simple code that moves box around using arrow keys

// update code called every frame
Moving.prototype.update = function(dt) {

pos = this.entity.getPosition();

 if ( {
     this.entity.setPosition(pos.x, pos.y, pos.z - 0.01);

Now my plan is to move the box according to the camera position (X/Z) instead of world
In other words, move the box towards the camera (Pressing KEY DOWN), away from the camera (KEY UP), no matter which way you look at the box, the movement must remain the same
What functions to use to calculate this movement/directions?

Hi @Newbie_Coder,

To do that you need to calculate the new world position based on the camera forward vector. The first person example works exactly like that for moving forward/backward and left/right (strafe).

Check how the final vector is calculated based on the camera forward/right direction. In your case instead of using it to calculate a force, you can use it on a entity.translate() method to move your cube.

1 Like