How to hold an entity to one axis

Hello,
I am wondering if it is possible to have one axis only equal 0. Like using

this.entity.setPosition(0,0,0)

All of the axisis would act normally but the one axis would only equal a variable or something like that. Is this possible?

Yes, that’s possible.

this.entity.setPosition(pos.x, 0, pos.z);

Make sure pos is a vector 3.

If you don’t want to change an axis at all, you can do something like below.

var currentPos = this.entity.getPosition();
this.entity.setPosition(pos.x, currentPos.y, pos.z);

How would I do that?

Below some examples.

var pos = new pc.Vec3();
var pos = this.entity.getPosition();
var pos = anotherEntity.getPosition();

Thanks a lot, this seems to work well! Is there a way to do this for rotation as well?

Yes, for rotation it works the same way. Instead of getPosition() you can use getEulerAngles().

Thanks! I’ll try that when I get a chance!

1 Like