Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Grid Framework [scripting and editor plugins]

Discussion in 'Assets and Asset Store' started by hiphish, Jul 24, 2012.

  1. coolbird22

    coolbird22

    Joined:
    Mar 20, 2014
    Posts:
    38
    Hi Hiphish,

    I came across your Grid Framework asset and really loved how it works. I would be interested in buying it though I have some doubts that I hope you might clear before I take the plunge.

    In this video below, you have shown how the sphere moves randomly through the grid and (maybe) avoids the grids that contain the walls. How is it possible to move the Player continuously in vertical/horizontal direction (by pressing WSAD keys) and only turn when the grid in front of it is a wall ? Also, suppose if the sphere is moving horizontally and I wish to make it go up after the wall, so ideally I would keep W pressed a little earlier than the actual turn to make it go up, but this might make the sphere collide with the wall instead and make it go in a different direction than required. How can this be achieved ? Is something like this possible ? It would be great if you could lend some suggestions as to how this can work. Thanks for taking the time to read this.

    Regards,
    coolbird22

     
  2. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,

    There is no collision detection used in the example, instead it uses a 2-dimensional array of bool values for free and occupied tiles. It works as follows: object wants to go into a direction, so it asks a manager object if the destination tile is free. If it is, then the movement is performed, otherwise nothing happens. Once the destination tile is reached the next destination is randomly generated. This means the sphere will not even attempt to move towards the wall tile.

    Continuous movement is no problem. In the example a new direction is generated randomly when the previously scheduled move is completed. Instead of randomly generating the move simply query the player's input and generate a new direction from that. The player can keep any keys pressed during the move process, the input will only be queried once the move has been completed. Take this drawing as an example:
    Code (csharp):
    1.  
    2. ......X
    3. ..O...X
    4.  
    (X is a wall) The player is holding D all the time, so after two moves it will look like this:
    Code (csharp):
    1.  
    2. ......X
    3. ....O.X
    4.  
    The player is still holding D to move right, but halfway through the move he realises he wants to go up after this move. Since he pressed W after the move has been initiated the input will only be processed once this move has finished:
    Code (csharp):
    1.  
    2. ......X
    3. .....OX
    4. =>
    5. .....OX
    6. ......X
    7.  
     
  3. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Hi,

    Is there any release date for version 1.5? I'm doing an isometric game so the shearing feature is one I am eagerly awaiting :)

    Thanks
     
  4. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    It's all ready to deploy, I'm just waiting for Unity to approve version 1.4.2 first, before I can submit 1.5. Version 1.4.2 is important because without the large refactoring the shearing feature would have been impossible. Maybe the entire Asset Store staff is still on vacation and they have just one unpaid intern who has to go through everything manually.
     
  5. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Yeah I saw your post on the blog, but for me the Asset Store is saying the current version is 1.4.2 since the 25th of August.
     
  6. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Great I'm not getting notifications about anything anymore. It used to be that I always get an email when my submission gets accepted or rejected. Thanks for the heads-up, I'll submit version 1.5 as soon as I can. I can send you the new GFRectGrid script per PM to patch you over for the time being, then you don't have to delay your work further.
     
  7. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Perhaps that is it, but double check on your end just in case I am wrong :)

    That would be great if you can send the script, would really appreciate it, thanks!
     
  8. andrewow

    andrewow

    Joined:
    Dec 2, 2013
    Posts:
    13
    I am having an issue with Scale Selected. I have an orthographic camera and a grid with 0.8 spacing. I am using scale selected with a Sprite. When I use "scale selected" in the grid align panel, the sprite is larger than the grid faces. In the attached photo, the center gray piece is the sprite I am trying to scale into the grid. As you can see it overlaps on all the edges. When I change the grid spacing to 1.0 this issue does not happen anymore.
     

    Attached Files:

  9. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    i'm pretty interested in the grid framework for it's ability to easily render a grid with minimal configuration. one particular problem i'm hoping it can solve for me is rendering partial areas of the grid. imagine i'm trying to draw up the grid for a cave - the cave layout is approximately circular, but it seems the grid framework is only capable of drawing out a rectangular region. unless i'm mistaken?

    In other words, I'd like to be able to flag certain cells in the grid as valid/invalid such that they won't be drawn. sorry if this is a feature the grid framework handles quite gracefully. I haven't dug into it very much. it seems like a non-trivial problem, hence my asking. Thanks for looking at this! 8)
     
  10. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Have you set the layers in the grid align panel to everything, or at least the layers you want to use? Try it out with a 1x1 cube and see if the same thing happens.

    Sorry, the built-in rendering techniques can only handle rectangular areas. What you are asking for is a more advanced task; it would be possible to render each tile individually, but then there would be a lot of redundant vertices and lines drawn twice. A good algorithm would be able to get the minimum set of vertices and edges. This would raise another problem though: the more rules you have, the more information the algorithm needs. A purely rectangular area needs only two points, allowing for fast adjustments, but control over individual tiles would scale upwards with the grid size.
     
  11. latas

    latas

    Joined:
    Oct 9, 2013
    Posts:
    149
    Hi,

    I'm interested in this Unity plugin but I would like to ask you a few things before buying it.
    I want to make some kind of "Puzzle building game", and the features I need are:
    1. Ingame Drag and Drop system into a grid.
    2. Ingame Snapping system with grid objects into the grid itself.
    3. Ingame grid items mouse highlighting.

    Does this plugin have these features?
    If so, is there any video/tutorial/documentation showing how to do it?

    Thanks.
     
  12. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    All those things are easily possible, but I should stress that Grid Framework is a framework, no a "just add water" solution. What this means is, to be able to drag & drop objects into the grid you will need your own drag&drop logic and Grid Framework will help you adjust the result to snap nicely.

    The usual way of doing click-dragging is to have a collider plane and then cast a ray from the cursor to see where it hits the collider. That point is where the player is pointing at. Once you have that point Grid Framework can take over:
    Code (csharp):
    1. Vector3 hitPoint;
    2. GFGrid grid;
    3. Transform myObjectTransform;
    4. Vector3 snapPoint = grid.AlignVector3(hitPoint);
    5. myObjectTransform.position = snapPoint;
    This is an intentional design decision, rather than forcing the programmer into my mindset I allow you to use Grid Framework in any way you want.

    To answer your questions, drag&dropping into a grid and snapping is pretty much the same thing and explained above. You write your code just as if you were not using a grid and then run the result through a method like `AlignVector3` or `AlignTransform` before applying the result. Highlighting tiles is similar, you have to provide how to highlight things, for example by drawing a box, and then you use the result of a raycast to position your highlight.

    There are a number of examples included with the package and on my YouTube channel I write and explain those examples live. I'll also send you the API documentation via PM, so you can take a look at what's included. The YouTube videos are pretty out of date unfortunately; they still apply, but the project was in a much rougher state back then.
     
  13. latas

    latas

    Joined:
    Oct 9, 2013
    Posts:
    149
    Ok, i'll give it a try :)
    Thanks for a fast answer.
     
  14. coolbird22

    coolbird22

    Joined:
    Mar 20, 2014
    Posts:
    38
    Thanks for the reply, Hiphish. I managed to get it to work with a simple 2D array instead. But, I really loved this framework. I'm sure to buy it soon, and recommend it to others. Thanks again.
     
  15. andrewow

    andrewow

    Joined:
    Dec 2, 2013
    Posts:
    13
    Yes, I have set the grid align panel to everything. I tried with a 1x1 cube and the scaling works. Perhaps this is a problem with Sprites only (I have Pixel to Units to 100).
     
  16. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The scaling will scale the object's Transform component, that's why it works with the cube. If your sprite is 100x100 pixels large and your pixel/units ratio is 100/1 it will work perfectly. However, if your sprite is say 150x150 pixels large and your ratio is 100/1 then your sprite will be 150 * (1/100) = 1.5 units large when it is unscaled. Scaling it to a 0.8x0.8 grid then yields a 1.2x1.2 large tile (1.5 * 0.8 = 1.2).

    You have to make the pixels/units ratio fit your sprite's size: if your sprite is nxn pixels large, then your ratio has to be n.
     
  17. latas

    latas

    Joined:
    Oct 9, 2013
    Posts:
    149
    Hi hiphish,

    I'm starting to use this framework and learning how to use it, but I'm not able to make the grid visible at runtime.
    I think I have set correctly the Grid, but obviously there is something wrong in my settings I can't find (see attached image).

    Thanks. WrongGrid.png
     
  18. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    You need to attach the GFGridRenderCamera component to your main camera: Components -> Grid Framework -> Camera
     
  19. latas

    latas

    Joined:
    Oct 9, 2013
    Posts:
    149
    Thanks, it works perfectly now :)


    Is there any way to make the grid always visible even if there are objects between the grid and the camera?
     
  20. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Sure, you will need two cameras to achieve that effect. Take a look at the Unity documentation for cameras, under clear flags -> depth only:
    http://docs.unity3d.com/Manual/class-Camera.html

    Instead of a gun you want a grid drawn on top of your scene. Create a second camera, you can make it a child of your main camera object and set its local position to (0,0,0) to make sure both cameras align. Attach the grid rendering component to your secondary camera and make sure the "render always" flag is set. That flag makes the camera render the grid even if it isn't the main camera. Remove the component from your main camera if you haven't already. Set the grid camera's clear flags to "Depth only" and set its depth to something higher than the depth of your main camera.
     
    Last edited: Sep 4, 2014
  21. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Grid Framework version 1.5.0 released

    Grid Framework version 1.5.0 has been approved by the Asset Store team. This version brings a great new feature for anyone interested in making isometric 2D games: shearing. Shearing allows you to slant a rectangular grid's axes without having to rotate the grid. Up until now the only way to achieve the isometric look was to rotate the grid or the camera in 3D space, but in 2D games the camera has to be perpendicular to the image plane and thus the grid as well. Now all you need to do is just set the shearing of the axes and you're good to go, everything else stays the way it was.

    The shearing is stored in a new type called Vector6 that works very much like Unity's own Vector3. The Vector6 class resides in the new GridFramework.Vectors name space to prevent name collision with other custom types or a possible future officialVector6 type from Unity.

    The GFBoolVector3 and GFColorVector3 classes have also been moved to that namespace and had their "GF" prefix stripped away. If you used them in your own scripts strip away the prefix and place a using directive at the start of your script, otherwise you have to do nothing.

     
  22. yuewahchan

    yuewahchan

    Joined:
    Jul 2, 2012
    Posts:
    309
    Instead of Intersect function, any method that can know one cube is adjacent to another cube ?
     
  23. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Something like this?
    Code (csharp):
    1. Vector3 cube1World, cube2World;
    2. GFRectGrid grid;
    3.  
    4. // convert world coordinates to grid coordinates
    5. Vector3 cube1Grid = grid.WorldToGrid(cube1World), cube2Grid = grid.WorldToGrid(cube2World), distance = cube1Grid - cube2Grid;
    6.  
    7. if (Mathf.Abs(distance.x) <= 1.01f)
    8.     Debug.Log("Horizontally adjacent");
    9. if (Mathf.Abs(distance.y) <= 1.01f)
    10.     Debug.Log("Vertically adjacent");
    11. if (Mathf.Abs(distance.z) <= 1.01f)
    12.     Debug.Log("Depthwise adjacent");
    Instead of using the distance 1 I added a bit of extra tolerance for rounding errors.
     
  24. dre788

    dre788

    Joined:
    Feb 15, 2013
    Posts:
    52
    Hey I'm getting errors in playmaker dealing with GFBoolVector3 and GFColorVector3 missing. Will there be an update for this?
     
  25. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm kicking myself right now for that oversight. I'll push an update as soon as possible.

    EDIT: submitted, waiting for approval now.
     
    Last edited: Sep 25, 2014
  26. VectorNector

    VectorNector

    Joined:
    May 22, 2013
    Posts:
    62
    Hey, Love this framework. I managed to get the Playmaker actions going but I can't seem to use it properly. I'm trying to use that Grid Align Transform action. Would anyone know how to use it?
     
  27. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I think I have it: In the script "Plugins/Grid Framework/PlayMaker Actions/FsmGFStateActionMethods" change the following lines:
    Code (csharp):
    1. // line 103
    2. public FsmObject transform;
    3. // to
    4. public Transform transform;
    5. // and line 107
    6. protected override void DoAction() { grid.ScaleTransform ((Transform)transform.Value, new BoolVector3 (ignoreX.Value, ignoreY.Value, ignoreZ.Value)); }
    7. // to
    8. protected override void DoAction() { grid.ScaleTransform (transform, new BoolVector3 (ignoreX.Value, ignoreY.Value, ignoreZ.Value)); }
    That should patch you over until the next update fixes it. The downside is that you can't use it with an FSM variable, I'll need to figure out how to do it.
     
  28. VectorNector

    VectorNector

    Joined:
    May 22, 2013
    Posts:
    62
    That didn't work...I think my problem is that I don't know how to use it without examples. I'm using the last version of GF before the latest and the Unity 4.5.4f1. If you can do a simple align transform scene, I'd be so grateful. The other actions work as intended, I'm just a moron. I can't seem to figure it out.
    Thank you!
     
  29. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'll walk you through the process of creating a minimum working example. I assume you know how to use Playmaker already.

    1) Create a rectangular grid and a cube
    2) Attach the FSM component to the grid object
    3) Attach the action to the first state of the FSM. The action has a number of fields, the most important one is Transform
    4) Drag the cube into the Transform field. This tells the action which Transform to align
    5) Hit play, and the cube should align. Make sure you have a good camera angle, ideally place the cube near the grid between grid lines, that way the effect will be most obvious

    When the game starts the FSM goes into the first state and executes the actions of that state. What the action does is align the position of the object you provided. Is there any particular reason you aren't using version 1.5.1 of Grid Framework? It's compatible with any version of Unity 3 and 4.
     
  30. VectorNector

    VectorNector

    Joined:
    May 22, 2013
    Posts:
    62
    I got the Grid Scale Transform to work and I used that fix you mentioned earlier. I noticed the errors in the console and fixed it accordingly. I thought I was going crazy for a second. I just wanted to verify that the actions work.

    I'm running 1.4.x because 1.5 was giving me errors so I used the my backup version. Once your latest build goes live, I'm going to play with it. I am very impressed by this Grid system. Even a dummy like me can understand it and take advantage of the features. I appreciate your help.
     
  31. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The latest release, version 1.5.1, is up since yesterday, I just haven't gotten around to announcing it.

    EDIT: and by the way, when I'm able to fix something quickly I usually send out the fixed script to patch people over until the official approval. If you are affected as well you can ask me for the script as well, I just don't want to post my code in public.
     
    Last edited: Oct 1, 2014
  32. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Grid Framework version 1.5.1 released

    This release is a hotfix for everyone who was using Playmaker
    • Fixed: Compilation errors when toggling on Playmaker actions.
    Playmaker actions wil compile correctly now, anyone else was unaffected. So what happened? One of the problems when developing an extension that relies on another extension is that it will not compile if the dependency is not present. For Vectrosity is was easy, I just needed to assemble and array and the user would then send it over to Vectrosity. Or not, you could do something entirely different with it as well if you wanted, it's just an array of Vector3.

    Playmaker is different though, the actions are classes derived from Playmaker classes, so if you don't own Playmaker the code will not compile. My solution was to do the following:
    Code (csharp):
    1. //#define PLAYMAKER_PRESENT
    2. #ifdef PLAYMAKER_PRESENT
    3. // code goes here
    4. #endif //PLAYMAKER_PRESENT
    This means the code will only compile if the keyword PLAYMAKER_PRESENT is defined, which it isn't since the first line is commented. To make the scripts compile you have to toggle them on through a menu item which then edits the scripts to un-comment or comment that line. Before I submit a new release I have to toggle the actions off and so I missed the compilation errors.

    It's still a bad oversight on my side and I'm not trying to excuse my failure, but I wanted to share how this can happen. A better solution would be if plugins could define keywords globally, then Playmaker could announce its existence on its own instead of having to toggle things manually.
     
  33. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    760
    I've just updated to 1.5.1, from a version quite a bit older (it's been a while, can't remember exactly which), and I've ended up with 2 'GFGrid.cs' files.

    One in: Plugins/ Grid Framework/
    And one in: Plugins/ Grid Framework/ Abstracts

    Which is the correct one to keep ?
     
  34. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The one in the Abstracts folder is the current one. I moved it there because it is an abstract class; back in the earlier days there were only five scripts, but as the number grew I had to sort them in sub-folders.
     
  35. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    760
    Thanks for the quick reply. The next error I'm getting is
    Assets/_Scripts/AddWaypoint.cs(42,27): error CS0426: The nested type `GridPlane' does not exist in the type `GFRectGrid'
    Code (csharp):
    1.  
    2. public GFRectGrid theGrid; // variable to store the grid being used
    3.     public GFRectGrid.GridPlane floorPlane = GFRectGrid.GridPlane.XZ; // set to use only the X and Z axis
    I've read the change log, which says:
    But I'm sorry, I don't fully understand how to re-write my code to be correct.
     
  36. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Quick answer: Add the following to the start of your script:
    Code (csharp):
    1. using GridFramework;
    and then change the type to plain Plane, nothing else, no GFGrid or GFRectGrid prefix. Technically you don't even need the using declaration, but then you'd have to write `GridFramework.Plane`.

    Long answer: In programming languages like C no two things may have the same name. So if there were two different types called "plane" that would be a compilation error. Objective-C "solved" that problem by adding a prefix, that's why every Apple class is called NSWhatever (NS Stands for NeXTStep). C++ on the other hand solved it with namespaces and other languages replicated the idea (Objective-C is older than C++).

    This means I can put my own types into a custom namespace so they won't collide with other types. If I were to declare a `Plane` type and another plugin did so as well, which `Plane` should the compiler choose? That's where namespaces come into play, the programmer can tell the compiler specifically which `Plane` to use. In order to avoid typing the namespace every single time the `using` declaration can be employed once per file.

    So why am I still using prefixes for the grid classes? It seems Unity has problems handling components that are in namespaces, so a prefix is the next best solution. Everything that's not a component though works fine from a namespace.
     
  37. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    760
    Wow, thanks for the detailed reply - much appreciated.

    I think I've updated incorrectly, I had another problem after sorting the last one out.
    I had to use
    'Plane' was coming back as part of the UnityEngine not GridFramework.

    I then found I had two lots of 'GFHexGridEditor.cs' and 'GFRectGridEditor.cs' ... I've removed the files from Editor/ Grid Framework and kept the ones in Editor/ Grid Framework/ Inspectors . Everything seems to be ok now.

    What's the correct way to update? I thought/ assumed Unity was a bit smarter with updating.
     
  38. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Usually Unity is a bit smarter when updating. If in doubt the easiest solution is to just delete everything that belongs to Grid Framework; the user manual has a list of all the relevant directories, but here is is again:
    • Editor/Grid Framework
    • Grid Framework
    • Plugins/Grid Framework
    • WebPlayerTemplates/Grid Framework Documentation
    Then import from the Asset Store again. Unity is able to import a package without re-downloading it again.
     
  39. VectorNector

    VectorNector

    Joined:
    May 22, 2013
    Posts:
    62
    No worries. You've done an incredible job making our lives easier and a little mistake won't change my opinion about this asset. I have other grid assets and it's pretty hard to get something simple running. So, this asset has allowed me to prototype faster. Thank you for your hard work!
     
  40. Riviere Delta

    Riviere Delta

    Joined:
    Aug 26, 2014
    Posts:
    8
    Hi Hiphish,

    I am new to Unity 3d. And I am working on a project where i am willing to use Grid Framework.
    I am attaching an image for reference.
    Here I want to move an object on the path.this path is a grid with different sizes of cells.
    Joint points which are highlighted using Yellow color, at those points the object should move on to all four directions with arrow key controls.
    And also, I want to restrict object off grid at the edges.

    Can I achieve this using Grid Framework? My main concern is those yellow joint points, where object should move to all directions.

    I request you to guide me if this is achievable using Grid Framework.
    I would really appreciate your help and guidance.

    Sincerely,
    Riviere Delta
    bg-with-path.png
     
  41. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hmmm, this would be more fit for a general graph solution since you want full control over your path nodes and you have only a finite set. Here is what a node class could look like:
    Code (csharp):
    1. public class PathNode : MonoBehaviour {
    2.     enum direction {north, east, south, west};
    3.     PathNode northNode, eastNode, southNode, westNode;
    4.     PathNode getNeighbouringNode(direction dir) {
    5.         if (dir == direction.north) {
    6.             return northNode;
    7.         } else if (...) {
    8.             ...
    9.         }
    10.     }
    11. }
    You save that script and attach it to your path node objects in the scene. A node can hold references for up to four other nodes and your actors (player, enemies, whatever) should hold a reference to their current node. If you want to move an actor convert the player input into a direction (north, east, south, west) and see if the corresponding node is not `null`. If it isn't move the actor over there (using the node object's Transform component) and set the reference to the new mode.

    None of this requires Grid Framework, it's just the most basic graph structure, your nodes don't even have to be on a grid, they can be anywhere. Now, if you want to place nodes on a grid, then Grid Framework can be of help. There is an editor panel that lets you align or auto-snap objects to a grid. Or you could use the Grid Framework API and do it in code instead of using the editor. That would require some coding, but you could then store your path graph in a file and then have your code assemble the nodes in the scene at runtime.

    Thanks for the kind words. I wanted mainly to explain how a compilation error could have slipped paste both me and the Asset Store team; shipping a bug is one thing, but shipping code that doesn't compile is unusual.
     
  42. Riviere Delta

    Riviere Delta

    Joined:
    Aug 26, 2014
    Posts:
    8
    Hi Hiphish,

    Thank you so much for detailed explanation. It's a great help.
    I haven't explored node yet.But now i will start working with nodes.
    I really appreciate your guidance.
    Thanks a ton once again.

    Sincerely,
    Riviere Delta
     
  43. VectorNector

    VectorNector

    Joined:
    May 22, 2013
    Posts:
    62
    Hey,
    I'm trying to build a grid of letter tiles and I'm looking at LevelTextParser.cs for reference. On line 59:
    Code (CSharp):
    1. Vector3 targetPosition = levelGrid.GridToWorld(new Vector3(i + shift + (levelGrid.GetType() == typeof(GFPolarGrid) ? 1 : 0), -row - shift, 0));
    I can't seem to figure why the numbers are negative. When I play the scene, the blocks y position values are positive. Can you help me understand what's going on? I can see the x value matching up.
     
  44. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    A text file starts in the upper left corner, but a grid starts in the lower left corner. To make the grid resemble the text file 1:1 I had to build the the level from the top-left corner as well. This means moving downwards through the grid coordinates every time I read the next line. Take a look at these text coordinates:
    Code (csharp):
    1. (0,0) | (0,1) | (0,2) | (0,3) | (0,4) | (0,5) | (0,6) | (0,7) | (0,8)
    2. (1,0) | (1,1) | (1,2) | (1,3) | (1,4) | (1,5) | (1,6) | (1,7) | (1,8)
    3. (2,0) | (2,1) | (2,2) | (2,3) | (2,4) | (2,5) | (2,6) | (2,7) | (2,8)
    4. (3,0) | (3,1) | (3,2) | (3,3) | (3,4) | (3,5) | (3,6) | (3,7) | (3,8)
    5. (4,0) | (4,1) | (4,2) | (4,3) | (4,4) | (4,5) | (4,6) | (4,7) | (4,8)
    6. (5,0) | (5,1) | (5,2) | (5,3) | (5,4) | (5,5) | (5,6) | (5,7) | (5,8)
    The first number is the row of the character and the second one is the column. However, in grid coordinates it looks like this:
    Code (csharp):
    1. ( 0,0) | ( 0,1) | ( 0,2) | ( 0,3) | ( 0,4) | ( 0,5) | ( 0,6) | ( 0,7) | ( 0,8)
    2. (-1,0) | (-1,1) | (-1,2) | (-1,3) | (-1,4) | (-1,5) | (-1,6) | (-1,7) | (-1,8)
    3. (-2,0) | (-2,1) | (-2,2) | (-2,3) | (-2,4) | (-2,5) | (-2,6) | (-2,7) | (-2,8)
    4. (-3,0) | (-3,1) | (-3,2) | (-3,3) | (-3,4) | (-3,5) | (-3,6) | (-3,7) | (-3,8)
    5. (-4,0) | (-4,1) | (-4,2) | (-4,3) | (-4,4) | (-4,5) | (-4,6) | (-4,7) | (-4,8)
    6. (-5,0) | (-5,1) | (-5,2) | (-5,3) | (-5,4) | (-5,5) | (-5,6) | (-5,7) | (-5,8)
    It's really just a personal decision, I find it more intuitive if the blocks are place the same way as they are written in the file.
     
  45. VectorNector

    VectorNector

    Joined:
    May 22, 2013
    Posts:
    62
    Wow, that makes sense now. I was trying to match up world coordinates during runtime. I should be thinking in grid coordinates. I appreciate you taking the time to illustrate that for me.
    Thank you!
     
  46. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    Do you have any suggestions on how I can ensure an entire grid (or just its width) will be seen on an orthographic camera?

    I'm using a custom render range, starting from 0,0,0 and what I render to can vary
    I'm using playmaker but could call a script if I have to
    There are objects in each grid square as well, if that helps
     
  47. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Orthographic cameras use a "size" property that tells us the height of the view rectangle and the width can be computed from the camera's aspect ratio.
    http://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html
    http://docs.unity3d.com/ScriptReference/Camera-aspect.html
    Code (csharp):
    1. Camera cam;
    2. float width = cam.orthographicSize * cam.aspect;
    We can then set the custom rendering range to go from the lower left-hand corner of the view rectangle to the upper right-hand corner:
    Code (csharp):
    1. Vector3 from = cam.transform.position - 0.5 * new Vector3(width, height, 0);
    2. Vector3 to   = cam.transform.position + 0.5 * new Vector3(width, height, 0);
    3.  
    4. GFGrid grid;
    5. grid.renderFrom = from;
    6. grid.renderTo   = to;
    There is an example included called "seemingly endless grid" where I do exactly that, I set the rendering range of the grid dynamically as the camera is moved to fit just inside the rectangle. When the player moves the camera the range adjusts, making it appear as if the grid was really rendered endlessly.
     
  48. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    Thanks for the reply. My problem is I'm already using renderTo to enable odd-dimensioned grids. My grids are also of sizes such as 5x5, 9x9, 8x14 but not limited to that.

    So my grids are fixed, not infinite. I need some way to get the total width and height of a grid and then fit the camera to that.

    Hopefully I'm making sense and not misunderstanding something fundamental here.
     
  49. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    In that case do the reverse, set the camera based on the grid. To fit the width of the grid compute the height of the camera:
    Code (csharp):
    1. height = width / aspect
    Then set the orthographic size of the camera to that value and the width of your view will be the width of your grid. Note that the height will be less, so you'll be missing something to the top and the bottom, but you said you wanted the width.

    The position of your camera is the centre of your grid:
    Code (csharp):
    1. Vector3 centre = grid.transform.position + 0.5 * grid.renderTo;
     
  50. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    I want to wrap a 2-D grid around a cylinder, and view it from the inside, is there a way to make this happen?