- Home /
suddenly have 'nan, nan, nan' error
Iv been following etteski FPS tutorials and have had no problems so, but now im suddenly getting Nan errors even tho i only changed a non important thing i added into the script.
this is the 'mouseLookScript'
var defaultCameraAngle : float = 60;
@HideInInspector
var currentTargetCameraAngle : float = 60;
@HideInInspector
var ratioZoom : float = 1;
@HideInInspector
var ratioZoomV : float;
var clampDown = -70;
var clampUp = 70;
var ratioZoomSpeed : float = 0.2;
var lookSensitivity : float = 5;
@HideInInspector
var yRotation : float;
@HideInInspector
var xRotation : float;
@HideInInspector
var currentYRotation : float;
@HideInInspector
var currentXRotation : float;
@HideInInspector
var yRotationV : float;
@HideInInspector
var xRotationV : float;
var lookSmoothDamp : float = 0.1;
@HideInInspector
var currentAimRatio : float = 1;
var headbobSpeed : float = 1;
@HideInInspector
var headbobStepCounter : float;
var headbobAmountX : float = 1;
var headbobAmountY : float = 1;
@HideInInspector
var parentLastPos : Vector3;
var eyeHeightRatio : float = 0.9;
function Awake ()
{
parentLastPos = transform.parent.position;
}
function Update ()
{
if (transform.parent.GetComponent(PlayerMovementScript).grounded)
headbobStepCounter += Vector3.Distance(parentLastPos, transform.parent.position) * headbobSpeed;
transform.localPosition.x = Mathf.Sin(headbobStepCounter) * headbobAmountX * currentAimRatio;
transform.localPosition.y = (Mathf.Cos(headbobStepCounter * 2) * headbobAmountY * currentAimRatio) + (transform.parent.localScale.y * eyeHeightRatio) - (transform.parent.localScale.y / 2);
parentLastPos = transform.parent.position;
if (currentAimRatio == 1)
ratioZoom = Mathf.SmoothDamp(ratioZoom, 1, ratioZoomV, ratioZoomSpeed);
else
ratioZoom = Mathf.SmoothDamp(ratioZoom, 0, ratioZoomV, ratioZoomSpeed);
camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, ratioZoom);
yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRatio;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRatio;
xRotation = Mathf.Clamp(xRotation, clampDown, clampUp);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
the only thing i changed was a variable i added called pause that meant when i pressed Esc i couldn't rotate my view, t worked perfectly no Nan errors but when i removed it i got Nan errors even tho it wasn't part of the original script and didnt effect the part giving me Nan errors.
I get this: !CompareApproximately (SqrMagnitude (q), 1.0F) UnityEngine.Quaternion:Euler(Single, Single, Single) MouseLookScript:Update() (at Assets/script/Player/MouseLookScript.js:69)
And this:transform.rotation assign attempt for 'playerCamera' is not valid. Input rotation is { NaN, NaN, NaN, NaN }. UnityEngine.Transform:set_rotation(Quaternion) MouseLookScript:Update() (at Assets/script/Player/MouseLookScript.js:69)
I also have errors with my player movement script because i use this for rotation.
If somebody could help me find out whats causing the errors and a way to fix that would be really helpful.
Edit: i tried copying and pasting directly from etteskis resources but still get the error even when i didn't before, i really don't know why it suddenly popped up.
FIXED: I think it was just a bug with unity, its a good engine but has a few weird bugs.
anyways, i fixed it by creating a new project and copying all files from old project, that's all. it was than fixed. I guess the error was just strangely caused when i edited the script (i hadn't edited that script in a while). It would be handy if these kinda bugs were fixed
NaN is often caused by a Divide By Zero situation.
playerCamera does not appear in the above script...
Is this script $$anonymous$$ouseLookScript??
playerCamera does not appear in the above script... Is this script $$anonymous$$ouseLookScript?? sorry, i forgot to say, its in the mouselookscript, the mouselookscript controls the player camera tho, and the problem is is that it worked fine than i removed something that i added that wasnt in the tutorial than i got this error and cant see whyNaN is often caused by a Divide By Zero situation.
Answer by ash_at_ReflexionHealth · Jun 17, 2014 at 05:43 PM
Check your parameters to SmoothDamp, it might use a division in there somewhere. Other than that, step line for line and see what the variables look like. The way nan works, once you get one it infects all the math you do after that, so if anything goes nan, everything that gets combined with it thereafter will be nan.
Answer by AndreiMarian · Aug 17, 2017 at 09:41 AM
It has to do with the internals of Unity engine and Euler Angles.
Check this out for a better understanding of what's going on: https://forum.unity3d.com/threads/how-to-fix-quaternion-lookrotation-error.200686/#post-1359340
Your answer
![](https://koobas.hobune.stream/wayback/20220613151737im_/https://answers.unity.com/themes/thub/images/avi.jpg)