- Home /
The question is answered, right answer was accepted
transform.TransformDirection() returns NaN
I have a child of a moving/rotating object, and I'm trying to determine my controller inputs relative to the child's local rotation. Using transform.TransformDirection(), I get a NaN result about once every 1,000 frames.
I also tried using trig to adjust the input vector against the child's transform.forward manually, with even more frequent NaN results.
I'm running this check in Update, running it in FixedUpdate caused much more frequent NaN results.
Neither the parent nor child's local rotation seem to affect frequency as currently implemented.
I have isolated the code to ensure it is the return value that is NaN, and not a result of later calculations.
Vector2 input;
Vector3 relInput;
void Update()
{
input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
relInput = transform.TransformDirection(input.x, input.y, 0);
Debug.Log(relInput);
}
Any help figuring this out would be appreciated. For now my temp workaround is to check the vector for NaN values and hold the previous frame's result for any frames that return NaN.
if input.x and input.y are both 0, this happens. Just check for both of them being null and return in that case (or just do not do the calculation)
I tried making the following change:
{
input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (input.x == 0 && input.y == 0)
relInput = Vector2.zero
else
relInput = transform.TransformDirection(input.x, input.y, 0);
Debug.Log(relInput);
}
It still returns NaN for relInput see$$anonymous$$gly at random.
Is this an actual codepaste?
Vector 2 input;
Vector 3 relInput;
If it is and you dont get Unexpected symbol error, something is wrong; scripts not recompiling.
No, if it was that would return a compiler error and not a NaN. But thank you for pointing it out, I'll correct it in the question.
Aye exactly. Was starting to think it was a clashing file issue :P If you use GetAxis ins$$anonymous$$d of Raw does it throw the error? Spurious readings are a possibility.
Answer by OmegaJeff · Mar 07, 2018 at 11:52 PM
After further examination, it appears to be a problem with either my controller or its driver. It is occasionally returning Infinity or -Infinity on GetAxisRaw.