- Home /
new Array storing rotations
I have a scenario where I am iterating through gameObjects and storing their locations and rotations in arrays like this:
for(var base in bases){
if(base.name == "type1"){
baseLocations1.push(base.transform.position);
baseRotations1.push(base.transform.rotation);
}
if(base.name == "type2"){
baseLocations2.push(base.transform.position);
baseRotations2.push(base.transform.rotation);
}
}
The issue I am seeing is that rotations are all stored as (0,0,0,1)...however, if those rotations are read into a builtin array like this:
gameManager.baseLocations1 = baseLocations1.ToBuiltin(Vector3);
gameManager.baseRotations1 = baseRotations1.ToBuiltin(Quaternion);
gameManager.baseLocations2 = baseLocations2.ToBuiltin(Vector3);
gameManager.baseRotations2 = baseRotations2.ToBuiltin(Quaternion);
then the rotations are stored correctly.
Can anyone tell me why I'm seeing this? I am trying to upload the Arrays as joined strings (using Array.join), then ultimately pull them down somewhere else and use them to place objects on a map. The upload/download works perfect, it's just that the format of the rotations is somehow getting zeroed out.
to clarify, a Debug.Log(base.transform.rotation) inside the for loop within the if statement shows the same thing, where the rotation is (0,0,0,1). And, like I said before, if this rotation is then stored in a builtin array, which explicitly types the data as Quaternion, the rotation then appears correctly (as viewed in the Editor Inspector). I can't see how the incorrect rotation of (0,0,0,1) in the JS Array gets to being correct in the builtin Array. Is the lack of typing in the JS Array the issue?
Answer by CHPedersen · Sep 09, 2011 at 06:27 AM
This had me scratching my head for a little while when I first started using Unity, too. It happens because the vector's ToString-method truncates the coordinates in Debug.Log when you pass in the entire vector/Quaternion at a time. Try to pass in the coordinates individually, like Debug.Log("x: " + base.transform.position.x + ", y: " + ... etc ) and you'll see that the coordinates aren't zero, you just weren't seeing the decimals before.
Thanks so much! I am now pushing to the Array like this, and it works great:
baseRotations1.push("(" + base.transform.rotation.x +","+base.transform.rotation.y+","+base.transform.rotation.z+","+base.transform.rotation.w+")");
Your answer
![](https://koobas.hobune.stream/wayback/20220612205354im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation along the x-axis to that player can turn around? 1 Answer
Gradual Rotation of Character from 2 Axis Input Sources 0 Answers
Smooth rotation in 90° increments 0 Answers
Euler angle problems with rotation limit script 3 Answers
Need help converting Euler-based rotation to Quaternion-based (gimbal lock). 1 Answer