Vector2 Not Setting Correctly

Hey Y’all,

I’m trying to rotate a Vector2 that starts at (1, 0). This code seems correct, and in fact checking the heading with Debug.Log(newHeading) and Debug.Log(this.heading) both confirm that the calculation is being done correctly.

HOWEVER!! the actual value of the heading that appears serialized in the inspector is something completely different (but consistent every time) that the object will actually use as its heading value. Here’s a snippet of what I’m working with:

Vector2 newHeading = heading;
if (behaviorList[i].values[0] == "0") { // TURN LEFT
   newHeading = new Vector2(heading.x * Mathf.Cos(Mathf.Deg2Rad*90) - heading.y * Mathf.Sin(Mathf.Deg2Rad*90),
                            heading.x * Mathf.Sin(Mathf.Deg2Rad*90) + heading.y * Mathf.Cos(Mathf.Deg2Rad*90));
   Debug.Log("after left rotation, " + newHeading);
   } else { // TURN RIGHT
   newHeading = new Vector2(heading.x * Mathf.Cos(Mathf.Deg2Rad*(-90)) - heading.y * Mathf.Sin(Mathf.Deg2Rad*(-90)),
                            heading.x * Mathf.Sin(Mathf.Deg2Rad*(-90)) + heading.y * Mathf.Cos(Mathf.Deg2Rad*(-90)));
   Debug.Log("after right rotation, " + newHeading);
   }
   this.heading = newHeading;
}

And this image shows what I mean – the heading is calculated correctly, yet the value that appears in the inspector is completely different [edit: forum will only let me upload one image, this is the more important one]:

image

Any help would be appreciated!! I have truly no idea what’s happening.

this is what the debug messages print – completely different from what the inspector shows:

image

Could be floating point inaccuracy. Copy and paste the X value to somewhere and it’s probably a very small value presented in scientific notation. Don’t expect floating-point values to be exact.

1 Like

Looks like that was it – thank you! Everything seems to work now, though unfortunately there was no clean way to keep everything as ints so I’m overcorrecting and doing a Lot of casting.