[SOLVED] Euler lock for PlayCanvas

I wanted to write the Unity function moveTowardsAngle() for PlayCanvas. I used purely Unity sources and there is no problem with the math library part, everything works as expected.

But when the y axis value reaches 90 in PlayCanvas, the x and z axis (I am trying to set value zero) get the value 180 and the rotation is locked.

Below is the code example where I am trying to zero:

let angleY = this.moveTowardsAngle(this.entity.eulerAngles.y, this.targetY, 50 * dt);
let newEuler = new pc.Vec3(0, angleY, 0);
this.entity.setEulerAngles(newEuler);

Console screenshot of the error:

I am attaching the SAMPLE PROJECT below. Do you have an idea about the cause or solution?

https://playcanvas.com/editor/scene/1466768

The euler angles are not stored on the entity. They are calculated from the transform matrix when you getEulerAngles which means they can only have a range of -180 → 180 on each axis and therefore you will see ‘flipping’ of number at certain angles.

You either have to store your working values for euler angles outside of the entity or work with Quaternions.

2 Likes

Thanks @yaustar, Instead of using entity.eulerAngles directly, I applied changes to it using an external vec3. In this way, the lock problem was solved.

let angleY = this.moveTowardsAngle(this.entityEuler.y, this.targetY, 50 * dt);
let newEuler = new pc.Vec3(0, angleY, 0);
this.entity.setEulerAngles(newEuler);
this.entityEuler.y = angleY;