How do I create a Multidimensional Associative Array of Vector3 values in C#?

I have a simple animated sprite object which I am trying to move to several predetermined starting points, and then perform a simple series of moveTowards “animations” (think whack-a-mole but with just one mole that shows up around random points on the screen). But in addition to the position, I also need to adjust the scale of the object and the rotation. So what I would like to do, unless there’s a better way, is to create a multidimensional array of positions, scale, and rotation that I can loop through randomly. My background is PHP, so I’m a bit confused on how to create this array.

Here’s what I’m trying to do:

Array data (
    Array (
        scale = Vector3(x,y,z),
        Array positions (
            Vector3(x1,y1,z1),
            Vector3(x2,y2,z2),
            Vector3(x3,y3,z3),
            Vector3(x4,y4,z4),
        ),
        Array rotations (
            Vector3(x,y,z)
        )
    ),
    Array (
        scale = Vector3(x,y,z),
        Array positions (
            Vector3(x1,y1,z1),
            Vector3(x2,y2,z2),
            Vector3(x3,y3,z3),
            Vector3(x4,y4,z4),
        ),
        Array rotations (
            Vector3(x1,y1,z1),
            Vector3(x2,y2,z2),
            Vector3(x3,y3,z3),
            Vector3(x4,y4,z4)
        )
    )
)

The order of positions and rotations is important. The scale will only have one value, but the positions will always have 4, and the rotations could have one or 4. I’m striving for best-practice methods, so I appreciate any and all suggestions!

Thanks in advance.

1 Answer

1

I’m not sure i understand what you actually want, but such nasty constructs are only possible in dynamic scripting languages. Unity uses Mono which is a compiled, strong-typed framework.

Besides the fact that this isn’t possible it’s also very unpractical. Having sometimes 4 and sometimes 1 rotation would require you to handle those cases seperately.

Usually it’s easier and the way of OOP to group together what belongs together. So you could make a class like this:

using System.Collections.Generic;

[System.Serializable]
public class StartingPoint
{
    public Vector3 position;
    public Vector3 rotation;
    public Vector3 scale;
}

[System.Serializable]
public class SimpleAnimation
{
    public List<StartingPoint> points;
}

And in your actual script you could create a List or array of SimpleAnimation:

public List<SimpleAnimation> animations;

However since position / rotation / scale is already defined in the Transform class you might want to use actual empty gameobjects to define your points, They can be easily edited in the editor.

Another way is to use the AnimationEditor to create some actual animations.

Thank you for the help! So are you suggesting that it would be better to place empty gameObjects with Transform info that I want to animate my sprite to? The classes you suggest above make sense to me, however the "points" property isn't showing up when I attach the script to a gameObject...

Well, it should. Are you sure you have the System.Serializable attribute on both classes?

Yes, I literally copied and pasted. The only difference is I also included using UnityEngine