More in this category:
Posted on April 27, 2014, by Richard Knol
In the next couple of weeks I will be posting some handy code snippets for use in your Unity3D projects. Nothing spectacular, but some of the functions can be quite useful.public static float To360Angle(float angle) {
while(angle<0.0f) angle+=360.0f;
while(angle>=360.0f) angle-=360.0f;
return angle;
}
public static Vector3 To360Angle(Vector3 angles) {
angles.x = To360Angle(angles.x);
angles.y = To360Angle(angles.y);
angles.z = To360Angle(angles.z);
return angles;
}
public static float To180Angle(float angle) {
while(angle<-180.0f) angle+=360.0f;
while(angle>=180.0f) angle-=360.0f;
return angle;
}
public static Vector3 To180Angle(Vector3 angles) {
angles.x = To180Angle(angles.x);
angles.y = To180Angle(angles.y);
angles.z = To180Angle(angles.z);
return angles;
}
public static float MathAngleToCompassAngle(float angle) {
angle = 90.0f - angle;
return To360Angle(angle);
}
public static float CompassAngleLerp(float from, float to, float portion) {
float dif = To180Angle(to-from);
dif *= Mathf.Clamp01(portion);
return To360Angle(from+dif);
}