Problem is not reproducible or outdated
Unity Math is Wrong for Some Reason
I'm getting really frustrated because I thought it was me that didn't get it, but after hours and hours of test and researching and checking, my equation is fine. Here's the deal;
I want to get the Cosine of my object's Y rotation. No, I didn't use transform.rotation; I used transform.eulerAngles like I should, and it corrects the magnitude to a 0-360 degree scale at runtime.
NO I AM NOT USING DEGREES. I take the Y rotation and multiply it by Mathf.Deg2Rad, then store that in a variable called tempR. After that, I run a simple test by Debug Logging the Cosine of tempR. There's only one problem; the wrong number comes out.
I tested it and the numbers are fine. I made tempR a public variable, and when my object is rotated 0d, tempR reads 0. When it's rotated 90d, tempR reads 1.57. 180d yields 3.14. This is correct. These are the right numbers for converting degrees to radians, so tempR does what it's supposed to.
The issue is when I plug tempR in Mathf.Cos. I'm not crazy. I take the cosine of 1.57 IN RADIANS on any calculator and I get the right answer. I plug 1.57 into Mathf.Cos IN UNITY and it debugs the correct answer. But when I plug in the cosine of tempR in Mathf.Cos it gives me a completely different answer, even when tempR mathmatically has to equal 1.57, and I see the freaking thing is 1.57 right there in the inspector at runtime. But no. Still the wrong answer.
But get this; when I harcode in "tempR is 1.57 and thats that" in the script, it works! Its the same number, so what makes it work one time but not the other? Can I not change the input for this basic math function at runtime? And it does the same thing with every other number as well. Even when I cut out the middle man and just try to find the cosine of transform.eulerAngles.y * Mathf.Deg2Rad, I still get the same wrong answer.
It's something simple, isn't it? It has to be something simple that i'm just not getting. It's driving me crazy. Anyways, thanks for hearing me out. Here's the code. Or at least a bare-bones version of what it's doing:
public float tempR;
void Update () {
tempR = transform.eulerAngles.y * Mathf.Deg2Rad;
Debug.Log (Mathf.Cos (tempR));
}
For a mathematically based question you have quite a bit of text and very little evidence that Unity is wrong. What is unity telling you?
Is your object a child of something that has a rotation? What is your hierarchy? Are you experiencing gimbal lock?
there's not much code to show. Its basic math that should work but doesn't. What I showed you is just about everything. "What is Unity telling you?" Common man, did you read the post, or did you just get triggered by the title? It can't be gimble lock or anything else because not only does the regular equation yield the right answer, but hardcoding the number yielded correct results. There's no such thing as gimble lock in a simple cosine calculation. Anyways, I dropped the project anyways, but thanks for trying
It clearly can't have been that big a problem for you if it took 3 months to respond!
When reporting a problem with code, you need to include a script together with all the input parameters supplied, the answer you expected, and the answer you actually got. Without that, we can't really help, because there's definitely nothing wrong with $$anonymous$$athf.Cos.
Follow this Question
Related Questions
Modifying number based on % change 0 Answers
Getting average value of slider while playing. 0 Answers
Predict the hit position 0 Answers
Check if a point is on the right or left of a vector? 2 Answers