- Home /
Array of Quaternions based of Array of GameObjects (NOT WORKING)
I am trying to get the rotation of every object in an array of gameobjects and put it in an array of Quaternions but when i do this it puts them in the Quaternion array as mere fractions of what the rotations were supposed to be.
The code that i thought would do this is
for (int i = 0; i < OBJList.Length; i++)
{
OBJloc[i] = OBJList[i].transform.position;
OBJrot[i] = OBJList[i].transform.rotation;
}
Based off a few google searches but i must of misunderstood something because this doesn't provide me with the rotation of each object in the array while it does work just fine for getting the Vector3 of each object.
How have you deter$$anonymous$$ed that the OBJrot[ i ] entries are a fraction of what is expected?
I was getting a list of objects during runtime and then my idea was that after storing all the data about the objects in an array i would save the array which consisted of a Int,Vector3,Quaternion and then be able to load it after restarting the game, now this is just me testing it all but here is the following code i was using...
This is where i got all the info to save: using UnityEngine; using System.Collections;
public class SaveOBJTESTwithtag : $$anonymous$$onoBehaviour
{
public GameObject PREFAB;
public GameObject $$anonymous$$aster;
public GameObject[] OBJList;
public Vector3[] OBJloc;
public Quaternion[] OBJrot;
public Vector7[] Pack;
void Start()
{
OBJList = GameObject.FindGameObjectsWithTag("OBJ");
OBJloc = new Vector3[OBJList.Length];
Pack = new Vector7[OBJList.Length];
OBJrot = new Quaternion[OBJList.Length];
}
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)) {
$$anonymous$$aster.Send$$anonymous$$essage("Ending", Pack);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C)) {
OBJList = GameObject.FindGameObjectsWithTag("OBJ");
for (int i = 0; i < OBJList.Length; i++)
{
OBJloc[i] = OBJList[i].transform.position;
OBJrot[i] = OBJList[i].transform.rotation;
}
for (int i = 0; i < OBJloc.Length; i++)
{
//Pack[i].location = OBJloc[i];
Pack[i].location.x = OBJloc[i].x;
Pack[i].location.y = OBJloc[i].y;
Pack[i].location.z = OBJloc[i].z;
}
for (int i = 0; i < OBJrot.Length; i++)
{
//Pack[i].rotation = OBJrot[i];
Pack[i].rotation.x = OBJrot[i].x;
Pack[i].rotation.y = OBJrot[i].y;
Pack[i].rotation.z = OBJrot[i].z;
}
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.B)) {
for (int i = 0; i < Pack.Length; i++) {
Instantiate (PREFAB, new Vector3 (Pack [i].location.x, Pack [i].location.y, Pack [i].location.z), Pack[i].rotation);
}
}
}
void LoadData(Vector7[] OldPackData)
{
Pack = OldPackData;
}
}
This is where i made the struct that i guess i needed in order to store the vector3 and quaternions in Binery Format using UnityEngine; using System.Collections; [System.Serializable] public class Vector7 { public int Item; public Vector3Serializer location; public SerializableQuaternion rotation; } [System.Serializable] public struct Vector3Serializer { public float x; public float y; public float z; public void Fill(Vector3 v3) { x = v3.x; y = v3.y; z = v3.z; } public Vector3 V3 { get { return new Vector3(x, y, z); } } } [System.Serializable] public struct SerializableQuaternion { public float x; public float y; public float z; public float w; public SerializableQuaternion(float rX, float rY, float rZ, float rW) { x = rX; y = rY; z = rZ; w = rW; } public static implicit operator Quaternion(SerializableQuaternion rValue) { return new Quaternion(rValue.x, rValue.y, rValue.z, rValue.w); } public static implicit operator SerializableQuaternion(Quaternion rValue) { return new SerializableQuaternion(rValue.x, rValue.y, rValue.z, rValue.w); } }
Answer by JVene · Aug 31, 2018 at 01:15 AM
The rest isn't formatted as code, so I haven't read that part, but this:
for (int i = 0; i < OBJrot.Length; i++)
{
//Pack[i].rotation = OBJrot[i];
Pack[i].rotation.x = OBJrot[i].x;
Pack[i].rotation.y = OBJrot[i].y;
Pack[i].rotation.z = OBJrot[i].z;
}
Doesn't copy all of the parameters inside the Quaternion. The commented line would copy a reference to the complete quaternion, and I haven't dealt with any other issues that may still be at work here, but Quaternions have a w parameter that isn't being copied, and that would produce incomplete results.
Perfect! after i changed it to
for (int i = 0; i < OBJrot.Length; i++)
{
Pack[i].rotation.x = OBJrot[i].x;
Pack[i].rotation.y = OBJrot[i].y;
Pack[i].rotation.z = OBJrot[i].z;
Pack[i].rotation.w = OBJrot[i].w;
}
Everything worked just fine.
Your answer

Follow this Question
Related Questions
Add two radius on the same shader 1 Answer
List of scripts? 2 Answers
How to get an array of transforms and move GameObject for certain duration to that transforms list 0 Answers
Isolation of different vertices on a mesh 2 Answers
Having trouble accessing a unique instance of a dynamically generated script... 0 Answers