.forward && .right functions

Hello, guys i used code i found on forum for virtual joystick character movement

had to rework it so it works in my project, but there is something i dont know to make work.

Here is function which makes my player move


As you can see it is using camera to move and orientate i guess.
And when camera is not centered with player then he is not running in straight line but it is trying to make it going straing related to camera position, and so movement is messed. I dont want to use camera to move character but the player, i tried to change this.camera .forward and this.camera.right for this.entity.forward and so as with right, but that didnt really help as the character was moving even more strange, it wasnt related to camera, but neither to the player i think, or maybe i dont know how to use that forward and right functions.

I also defined those in initialize

this.force = new pc.Vec3();
this.right = new pc.Vec3();

Here is full code: https://playcanvas.com/editor/code/733989?tabs=39056987

But you as well can use link to editor: https://playcanvas.com/editor/scene/1130613

does anybody know how to change it properly, or does anybody see any issue in the code? Thank you

To exactly understand my problem, please, hit play button and try to go forward.

Hi @smokys,

You need to use the forward direction of your player model, which is a child of your player entity. That is the one that rotates towards the direction of movement.

And you don’t need to use anything else, this works for me great in your game:

PlayerMovement.prototype.Move = function(){
    var playerModel = this.entity.findByName('PlayerModel');

    var force = this.force.copy(playerModel.forward).scale(-this.speed * 10);
    this.entity.rigidbody.linearVelocity = force;

Many thanks! That actually makes sense but i never used that forward and right funcs, you taught me some thing now :stuck_out_tongue: . Thanks again!

1 Like