[SOLVED] Set position by script

I want to move my character one unit backwards.

I try to use this:

this.entity.setPosition(new pc.Vec3(this.entity.getPosition().x, this.groundPoint.y, this.entity.getPosition().z + 1));

But this works only when the rotation of my character is (0,0,0).

If the rotation of my character for example is (0,90,0) my character is going one unit to the left…

You are setting and using world space coordinates. So adding +1 to the player’s Z property is going to move it along the world’s Z axis regardless which way the character is facing.

If you want to move the character relative to the player’s rotation, you have to use it’s local axis which is what translateLocal does (hence local in the name).

Typically in games, we use the forward vector property of the entity (which represents the -Z axis of the player) and move the entity along that vector to move forward and back.

I understand the problem, but I don’t understand the solution. :see_no_evil:

My logic (which I know isn’t right) would be:

this.entity.setLocalPosition(new pc.Vec3(this.entity.getLocalPosition().x, this.groundPoint.y, this.entity.getLocalPosition().z + 1));

Not the most elegant way, but I added an empty entity to the player and set the player on that position when the climbing is finished.

This is an example of moving an entity along the entity’s forward vector (forward and back). Use arrow keys: https://playcanvas.com/editor/scene/924245

Thanks. That is also working. I mark the question as solved.