Search Unity

Saving multiple locations for a Gameobject to move to

Discussion in 'Scripting' started by BearSheriff, Aug 26, 2016.

  1. BearSheriff

    BearSheriff

    Joined:
    May 14, 2015
    Posts:
    20
    I am currently making a GearVR app where I have multiple panoramas of a building, when the player gazes at an arrow he goes to the next panorama and the arrows are re-positioned. However the way I have it set up now is each new panorama is a new scene and I manually set the locations of the arrows to match up , I would like to make each panorama just a new material that I can swap out, but the only problem is I am not sure how to re-position the arrows. So how do I save multiple locations for the arrows?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    so something akin to googlemaps streetview?

    At it's most basic storing positions can either be done by have a collection (list/array/etc.) of Vector3 values, or Transforms.

    having said that you might consider a struct/class which holds the position Vector3, the material and other associated data instead for each point.
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    is there a set number of arrows that is static in each panorama? Does a paticular arrow always go to the same panorama that is arrow2 always takes me to panaroma 2 no matter which panorama i'm in?
    Or does there behaviour change based on the panorama like a forward and back button. they would work relative to which panorma i'm in.
     
  4. BearSheriff

    BearSheriff

    Joined:
    May 14, 2015
    Posts:
    20
    There are not a set number of arrows, that depends on the room and how many pathways there are. Each arrow will go to a different room in the building, its relative to the panorama you are in. Thanks for helping!
     
  5. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    So doesn't sound like you need to think of them as the "Same" arrows so you don't need multiple positions. I would create some data structure Panorama that has the following things in it:
    1. Material you need to load in
    2. The number of arrows total
    3. List of position data for each arrow.
    Then you need to create an array/list of these Panorama structs. Then You call a function
    LoadPanorama(index) This function can load the material for the new panorama, Check the number of arrows needed and destroy extras, create new ones if short. (though instead of destroying extra arrows you can also just disable them and send them to a pool to get back later. this help on constantly creating/destroying an arrow object). Then once your arrow object number is correct , just iterate through your list and set all the position of the arrows.

    The data for the arraylist, would just be hardcoded in a file somewhere, and you create your list at the game's startup.
    You will probably need to get more advanced and create an Arrow data structure that includes its position data, as well as infomration on where it goes to when looked at. then replace #3 above with that data struct.
     
  6. BearSheriff

    BearSheriff

    Joined:
    May 14, 2015
    Posts:
    20
    Hey thanks for the reply. I mostly get what you are saying, but I am confused on how to change the transform of the arrows to fit the skybox. Each arrow either needs to be in different positions depending on the panorama (or be disabled) I am in, but I do know how to store these positions. For instance when skybox[0] is the skybox arrow1 should be in 0,0 20 position, but when skybox[1] is active arrow1 should be in a new position.
     
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Here's some code to give you an idea of what I mean. Its won't compile its just rough ideas of what you need to do.
    Code (CSharp):
    1.    public struct ArrowInfo
    2.     {
    3.         public Vector3 position;
    4.         public var placeToGo;  // some index to where the arrow points to, or some function ptr
    5.     }
    6.  
    7.     public struct PanoramaInfo
    8.     {
    9.         public string MaterialName;
    10.         public int numberArrows;
    11.         public List<ArrowInfo> arrows;
    12.     }
    13.  
    14.     // lets say we had 10 panoramas
    15.     public PanoramaInfo[] panoramas = new PanoramaInfo[10];
    16.     public void LoadPanoramaInfo()
    17.     {
    18.         ArrowInfo arrow = new ArrowInfo();  // just a temp variable to store arrows data
    19.  
    20.         // this entire function is loading variables with data that is hard coded
    21.         // its positions you worked out for the arrows by hand on each panorama and saved them.
    22.         // Ideally these number would all be in a file you read from. and used loops to get all the data
    23.         panoramas[0].MaterialName = "MaterialOne";
    24.         panoramas[0].numberArrows = 5;
    25.         panoramas[0].arrows = new List<ArrowInfo>();  // have to initialize it here since structs can't initialize.
    26.         // load the 5 arrow positions and save them
    27.         arrow.position = new Vector3(10,10,0);
    28.         arrow.placeToGo = // whatever you need it to
    29.         panoramas[0].arrows.Add(arrow);
    30.  
    31.         arrow.position = new Vector3(10, 20, -10);
    32.         arrow.placeToGo = // whatever it neds to be
    33.         panoramas[0].arrows.Add(arrow);
    34.         // Do this 5x total.
    35.  
    36.        // then yo uwould start on next panoroma
    37.        panormas[1].MaterialName = "MaterialTwo";
    38.       //and this would be just like the panormas[0] code but with differnt arrow number/positions
    39.     }
    40.  
    41.     // this will setup and load your panorama based on an index you send it.
    42.     public LoadPanorama(int index)
    43.     {
    44.         LoadYourMaterialFunction(panoramas[index].MaterialName);
    45.  
    46.         // However your program stores the arrows here is where you check you have enough made
    47.         if (NumberofArrows < panoramas[index].numberArrows)
    48.             // Do some stuff to instantiate more arrows
    49.         if (NumberofArrows > panoramas[index].numberArrows)
    50.                 // Do some stuff to destroy extra arrows
    51.  
    52.        //You code some way to get a list of all your UI ArrowObjects
    53.        //this is just pseudo code to give you an  idea of what i'm talking about
    54.        ArrowObjects[] arrowsInScene = GetComponents<ArrowObjects>();
    55.  
    56.         for (int i=0;i<panoramas[index].numberArrows;i++)
    57.             arrowsInScene[i].transform.position = panoramas[index].arrows[i].position;
    58.     }
     
  8. BearSheriff

    BearSheriff

    Joined:
    May 14, 2015
    Posts:
    20
    Thank you! This helps a ton!