Mathf.Acos(-1) return NaN?

Vector3 targetDir = pos - transform.position;
Vector3 tempDir = Vector3.Cross(transform.forward,targetDir.normalized);
Vector3 tempDir = Vector3.Cross(transform.forward, targetDir.normalized);
float dotValue = Vector3.Dot(transform.forward, targetDir.normalized);

float value = Mathf.Acos(dotValue);

when the dotValue == -1, Mathf.Acos(dotValue) Sometimes returns NaN,Sometimes the result value is (π == 3.141593 )

ACos is only defined for the range of input values between -1 and 1. I suspect that the result returned by Vector3.Dot can sometimes go slightly outside this range (even though your input vectors are normalised) because of calculation imprecision - it might still be being printed when rounded as “-1” though.

Try adding something like this before your Vector3.Dot calculation:

if ( dotValue > 1.0f )
    dotValue = 1.0f;
else if ( dotValue < -1.0f )
    dotValue = -1.0f;