Failing to use Atan2 with Sin/Cos, need some help.

Hello, I’m trying to convert a 2008 engine character controller to Unity3D, but I’m having some problems with those trigonometric functions, Sin and Cos:

float flDeltaCos = Mathf.Cos(flAnimDirection-90);
float flDeltaSin = Mathf.Sin(flAnimDirection-90);

Then If my speed length is greater or equal than 0.1:

flAnimDirection = Mathf.Atan2((vAcceleration.x+flDeltaCos*flSpeed_Length*6f),-(vAcceleration.z+flDeltaSin*flSpeed_Length*6f));

That’s the original unchanged code, Acceleration is always relative to the camera, so If I press W or the Up directional It will accelerate on the camera’s facing direction.
The original engine uses Euler angles I guess (0 to 360), The values are just changing really fast, They were supposed to slowly change relative to my acceleration, I also tried multiplying Atan2 results for Mathf.Rad2Deg but the character just spins uncontrollably.

flDeltaCos and flDeltaSin are also used for the ‘turning’ speed compensation which looks like:

vSpeedCompensation.x = (vSpeed.x*11f+flDeltaCos*flSpeed_Length)/12f;
vSpeedCompensation.z = (vSpeed.z*11f+flDeltaSin*flSpeed_Length)/12f;

Thanks!

At a glance, looks like deg/rad conversion problems. Cos and Sin work like the “real” math functions, they take radians, 0 is “east” (which I think you have) and go CCW.

If flAnimDirection is in “unity degrees” (0 is north, degrees, going CCW, so 90 is east) the conversion is, hmmm…from memory, (90-flAnimDirection)*Mathf.Deg2Rad

Likewise, Atan2 returns proper math radians (of course, the inputs are just lengths, so no conversion ever needed.)