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. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Mathematically yes, but doing it with a rectangular grid would require you to write your own mapping code that maps the planar grid onto a curved surface. It would be easier to just use polar grids instead, that's the sort of thing they have been designed for.


    That will make the grid visible on the outside. To make it visible on the inside make the radius of the grid slightly smaller than the radius of your cylinder.
     
  2. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    Awesome, can you give me the code you used to make that demo?

    Just purchased this from the asset store and getting a bunch of errors in Unity 5, any idea when you'll be upgrading the scripts for the new version?

    Thanks
     
    Last edited: Nov 5, 2014
  3. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I don't have access to the Unity 5 beta, it's for pro customers only. If you tell me what errors you're getting I can try to fix them blindly. Unless Unity 5 has some really big API changes there should be no reason Grid Framework wouldn't work, it's written in a very minimal style.

    There is no code for the example, it was all done in the editor. Make the grid and XZ grid (if you cylinder is standing), set the rendering range to go from `x = radius of the cylinder - 0.01` to `x = radius of the cylinder + 0.01` (we add a little bit to both ends because of floating-point imprecision), the y goes from `-height of the cylinder` to `+height of the cylinder` and the z goes from 0° to 360°. For best results the smoothness setting of your grid should match the smoothness of the cylinder mesh, but that's just for aesthetics.
     
  4. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    Just a bunch of depreciated code changes - make these changes and you should be good - I got it working in 5


    Line 744 and 757 in GFGrid.cs
    if(cam.orthographic)

    Line 39 in LightsBehaviour.cs
    achedRenderer = GetComponent<Renderer>();

    Line 32 in PointDebug.cs
    col = GetComponent<Collider> ();

    Line 140 in SnappingUnits.cs
    Rigidbody rb = GetComponent<Rigidbody> ();

    Line 159 in SnappingUnits.cs
    Collider col = (Collider) go.AddComponent(GetComponent<Collider>().GetType());

    Line 124 and 126 in SnappingUnits.js
    GetComponent.<Renderer>().material = redMaterial;
    GetComponent.<Renderer>().material = defaultMaterial;

    Line 131 in SnappingUnits.js
    var rb : Rigidbody = GetComponent.<Rigidbody>();

    Line 150 in SnappingUnits.js
    var col : Collider = go.AddComponent(GetComponent.<Collider>().GetType()) as Collider;

    Line 31 in PointDebug.js
    col = GetComponent.<Collider>();
     
  5. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Thanks, I did it. Looks like they got rid of the accessors that were secretly calling GetCoponent. Good riddance, those were a real beginner's trap.
     
  6. hiphish

    hiphish

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

    Grid Framework version 1.5.2 has just been approved of by the Asset Store team. This release is a bugfix for polar grids where the cylindric lines have not been calculated properly if the depth of the grid was not 1.

    Another thing you might notice is that the engine requirements have been knocked down to Unity version 3.5.7, the latest release of Unity 3. While Grid Framework has always been compatible with Unity 3, I have been using Unity 4 for deployment so far. Now the compatibility with Unity 3 is official.

    Compatibility with Unity 5 is coming with the next update; it's nothing major, but some of the examples did not compile due to deprecated properties.
     
  7. hiphish

    hiphish

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

    Grid Framework version 1.5.3 has been approved by the Asset Store team. This release brings compatibility with the upcoming Unity 5.

    Grid Framework itself was compatible, but a number of examples used properties that have been removed in Unity 5. Those are properties that secretly called GetComponent, but looked like regular variables.

    And while I'm at it, here is a little teaser for the next feature update: rhombic hex grid rendering
     
  8. ashenbee

    ashenbee

    Joined:
    Jan 28, 2014
    Posts:
    11
    Hello, thanks for the great asset. I'm using this for a hex-based strategy prototype. I read earlier in the post how to use Vectrocity to highlight a single grid. I'm having issues trying to get this to work with highlighting a single hex (only in 2d plane).

    I'm using a custom script attached to the grid object that displays the current grid coordinates to a GuiText object (which works fine), but I'm unable to figure out how to get a single hex outline's points to work with Vectrocity (the hex in question is the hex underneath the mouse cursor). It selects one or more hexes nearby as seen in the attachment.

    highlight.png
    Code (CSharp):
    1.  
    2. var mouseWP = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    3.  
    4. Vector3 face = grid.NearestFaceW(mouseWP);
    5. Vector3[] highlightPoints = grid.GetVectrosityPoints(face, face+ Vector3.one);
    6.          
    7. VectorLine.Destroy(ref highlight);
    8. highlight = new VectorLine("HexHighlight", highlightPoints, null, 1.0f);
    9. highlight.Draw();
     
  9. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    It would be easier to go the direct route, i.e. use the face as a basis and add six vectors to it:
    Code (csharp):
    1. Vector3 face = grid.NearestFaceW(mouseWP);
    2. Vector3[] highlightPoints = new Vector3[6] {
    3.     face + grid.radius * Vector3.right,
    4.     face + 0.5f * grid.radius * Vector3.right + 0.5f * grid.height * Vector3.up,
    5.     face - 0.5f * grid.radius * Vector3.right + 0.5f * grid.height * Vector3.up,
    6.     face - grid.radius * Vector3.right,
    7.     face - 0.5f * grid.radius * Vector3.right - 0.5f * grid.height * Vector3.up,
    8.     face - 0.5f * grid.radius * Vector3.right + 0.5f * grid.height * Vector3.up,
    9. };
    The Vectrosity support could use some more love though. I'm working on rendering anyway right now, so I'll add it to the list for the next release.
     
  10. ashenbee

    ashenbee

    Joined:
    Jan 28, 2014
    Posts:
    11
    Thanks for the rapid reply! With a small modification to the points, this now works like a charm:

    Code (CSharp):
    1. mouseWP = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2. Vector3 face = grid.NearestFaceW(mouseWP);
    3. highlightPoints[0] = face + grid.radius * Vector3.right;
    4. highlightPoints[1] = face + 0.5f * grid.radius * Vector3.right + 0.5f * grid.height * Vector3.up;
    5. highlightPoints[2] = face - 0.5f * grid.radius * Vector3.right + 0.5f * grid.height * Vector3.up;
    6. highlightPoints[3] = face - grid.radius * Vector3.right;
    7. highlightPoints[4] = face - 0.5f * grid.radius * Vector3.right - 0.5f * grid.height * Vector3.up;
    8. highlightPoints[5] = face + 0.5f * grid.radius * Vector3.right + 0.5f * grid.height * Vector3.down;
    9. highlightPoints[6] = highlightPoints[0];
    10.  
    11. VectorLine.Destroy(ref vectHL);
    12. vectHL = new VectorLine("HexHighlight", highlightPoints, Color.yellow, null, 2.0f, LineType.Continuous);
    13. vectHL.Draw();
    highlight2.png
     
  11. divyansh

    divyansh

    Joined:
    Nov 27, 2014
    Posts:
    1
    Hello,

    I want to move and drag drop block over grid or place out of grid or other grid.
    How can i do this.
    I purchase the library code but i can't go through it..
    Please help me..
     
  12. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Do you mean something like Snapping Units example that's included? The idea is the following:
    Code (csharp):
    1. This has all to be done within one frame
    2. 1) Get the position the object would be moved to without snapping
    3. 2) Get the nearest vertex/face/box of that position
    4. 3) Set the nearest vertex/face/box as the position of the object
    How you want to implement drag&drop is up to you. Then, when you would move the object normally you insert the steps above to correct the position to the snapped one. Look at the NearestVertex/Face/Box functions in the API documentation.

    The documentation can be found under Help -> Grid Framework documentation. There is no need to read the code of Grid Framework, unless you really want to of course, but the classes are very big.
     
  13. destapp

    destapp

    Joined:
    Oct 22, 2012
    Posts:
    18
    hi hiphish
    can i create endless grid Y axis only?
     
  14. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    You always have all three axes, but you can choose which ones you want to display and which ones not. In other words, yes you can.
     
  15. kallikantzaros

    kallikantzaros

    Joined:
    Dec 4, 2014
    Posts:
    2
    Hi HiPhish,

    Recently purchased, looks like a great framework. Just wondering if you had some examples of creating grids programmatically (ie new GFRectGrid(?)) All I have been able to find assumes that the grid exists in the scene already.

    Cheers,

    kalli
     
    Last edited: Dec 4, 2014
  16. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,
    All grids inherit from the GFGrid class, which in turn inherits from the MonoBehaviour class, so they behave just like any other Unity component.
    http://docs.unity3d.com/ScriptReference/MonoBehaviour.html

    You can create any grid using the standard constructor:
    Code (csharp):
    1. GFRectGrid myGrid = new GFRectGrid();
    The abstract classes GFGrid and GFLayeredGrid cannot be instantiated because they are abstract.

    You can also add a newly created grid to any GameObject just like any other component:
    Code (csharp):
    1. GameObject go;
    2. GFRectGrid grid = go.AddComponent<GFRectGrid>();
    http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html
     
  17. kallikantzaros

    kallikantzaros

    Joined:
    Dec 4, 2014
    Posts:
    2
    Thanks mate! The comments about developer support were the main reason I settled on this framework, see they were well founded.
     
  18. Thenegotiator

    Thenegotiator

    Joined:
    Jul 12, 2014
    Posts:
    1
    Hello Hiphish I am sorry but I can not seem to get around a problem I am having with your Asset when I try to compile it it says there are 17 errors in GFGrid.cs, 1 error in GFVectorTwoExtensions.cs, 3 errors in GFHexGrid.cs, 8 errors in GFPolarDrid.cs, and 1 error in GFRectGrid.cs. for a total of 30 errors. I can't seem to fix them, and my code compiles correctly with no problems at all. I have tried updating Unity and reimporting all and still no fix. Sorry I am fairly new to coding only knowing basics, so I may have missed something, but thank you for your time and your great asset. It used to work awhile ago and that's why I say it is great for the time I was able to use it. If you need My whole project tell me where to send it too. And thanks again.
     
  19. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Can you please tell me what the compilation errors are and what version of Unity you are using? I need that information or I cannot help you. The Unity Asset Store team always checks if the code compiles, so I am surprised by that.

    I like getting feedback. Some things, like creating grids programatically are trivial from my perspective, but they might not be obvious for someone new. I added this information to the user manual, it will be included in the next release.
     
  20. hiphish

    hiphish

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

    Version 1.7.0 of Grid Framework has just been approved by the Asset Store team. This release brings some more love for hexagonal grids in the form of two new coordinate systems and four new rendering shapes.

    The new coordinate systems are the downwards herringbone coordinate system where every second column is shifted down- instead of upwards and the downwards rhombic coordinate system where each successive column is shifted one hex down- instead of upwards.

    Two of the new rendering shapes are downwards rectangle and downwards rhombus to go with the new coordinate systems. The two completely new rendering shapes are the up- and downwards herringbone grids. Unlike the other shapes these ones are continuous and expand smoothly.
    Notice how the grid on the left-had side shifts ever odd column upwards and the grid on the right-had side shifts ever odd column downwards.

    Here is the full change log:
    • New: Downwards herringbone coordinate system for hex grids
    • New: Downwards rectangle rendering shape to accompany the new coordinate system.
    • New: Downwards rhombic coordinate system.
    • New: Downwards rhombic rendering shape to accompany the new coordinate system.
    • New: Up- and downwards herringbone rendering shape.
    • Fixed: The grid align panel now correctly respect or ignores rotation when aligning.
     
  21. xdotcommer

    xdotcommer

    Joined:
    Aug 20, 2014
    Posts:
    33
    Does GridFramework work on iOS devices? I am working with GridFramework together with Vectrosity and had a really cool effect going, but it doesn't display at all when I put it on my iPhone 6.
     
  22. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Grid Framework just sends an array of vectors to Vectrosity, nothing platform-specific, so it should work everywhere. Have you tried using Vectrosity on you iPhone without Grid Framework? It could be an issue on that end.
     
  23. Onoma

    Onoma

    Joined:
    Dec 16, 2014
    Posts:
    3
    Hi, I just bought Grid Framework, is great but i´m struggling to implement pathfinding. Do you plan to add pathfinding? Please give a rough estimate, if possible.

    Thanks!
     
  24. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi, there are still other things to do before tackling pathfinding. I'm sorry, but I don't have even an estimate. There are other pathfinding solutions for Unity and they use a navigation graph. You can easily create a set of graph vertices from a grid like this:
    Code (csharp):
    1. GFGrid grid;
    2. List<Vector3> nodes;
    3. for (int i = xMin; i < xMax; +++i) {
    4.     for (int j = yMin; j < yMax; +++j) {
    5.         for (int k = zMin; k < zMax; +++k) {
    6.             nodes.add(grid.GridToWorld(new Vector3(i, j, k)));
    7.         }
    8.     }
    9. }
    Then you can pass these nodes to the pathfinding solution and let it take over from there.
     
  25. Onoma

    Onoma

    Joined:
    Dec 16, 2014
    Posts:
    3
    Hi, thank you very much for the quick response.
    Could you show me which of these solutions would be suitable for your framework in a turn based roguelike rpg?

    Thanks!!
     
  26. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I know of this one, but I never tried it myself, so don't take my word as recommendation.
    http://arongranberg.com/astar/

    There are probably others out there as well, you should ask the Unity community and the respective developers. Getting the coordinates for the graph is just a matter of converting the grid coordinates you want to use to world coordinates.
     
  27. Onoma

    Onoma

    Joined:
    Dec 16, 2014
    Posts:
    3
    Thank you very much Hiphish
     
  28. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    I think the option to toggle on playmaker actions has disappeared from the component menu. Has it been moved?

    EDIT: switched to iOS platform and Grid Framework popped up in the component menu. Weird
     
    Last edited: Jan 14, 2015
  29. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Yes, Unity is sometimes weird that way. I don't know why, but sometimes my menu items disappear randomly. This happens regardless of whether I change and code or not.
     
  30. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    A homepage for Grid Framework

    Grid Framework now has its own homepage:
    http://hiphish.github.io/grid-framework/

    I have been running the blog now for almost three years, which is way too long to go without a proper website. Back when I had just started out I didn't know if Grid Framework would ever take off, so I organised everything in a quick&simple manner, but I think it's finally time to step up my presentation.

    The site is hosted on GitHub, so you can browse the source code if you wish, it's all handwritten in HTML and CSS, nothing fancy. There are still a few things I would like to overhaul, like a proper screenshot gallery, playable examples and redo the videos. But for now the site is at least presentable.

    If you have any suggestions or find display errors on some devices please let me know.
     
  31. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    Hi. I am interested in purchasing this product. Does this work with 4.6 UI? I would like to use the 4.6 containers such as a Panel since they have anchors etc. The problem is that I have 3d objects like cubes in my Grid and I can't place them in the Canvas/Panel since that is 2d, but if I place those cubes in your grid which is 3d, will I be able to place the grid properly in a panel and anchor it to the panel, so its size changes according to the panel.

    Is it possible to snap irregular pieces to the grid via script such as a Triangle, which is 2 height by 1 base? In this case the base should snap to 1 cell and the height to 2 cells.

    Also, do you have a full sample game where all the functionality is achieved through scripting?

    Thanks,
    Syed
     
  32. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    That's a tough one. The new UI is in screen space, not world space, whereas the grid is in world space. You could make things line up in the editor. You can also make the grid object a child of the Canvas object and it will follow. However, connecting the grid to the Canvas to resize with it is the tricky part. Not impossible and probably not too hard either, but I haven't looked into it yet and I would have to read through all the documentation. I am putting it on the feature list. It would look something like this:
    Code (csharp):
    1. myGrid.size = someCalculation(myCanvas);
    You can snap anything as long as it has a Transform, but in such a case you would need to define your own rule. Assuming the pivot point of you triangle is at the centre your code would be
    Code (csharp):
    1. Vector3 targetPosition = myGrid.AlignVector3(trianglePosition, new Vector3(1, 2, 0));
    The first argument is the position of the triangle, the second an imaginary "scale" that was chosen according to the alignment rules. It's explained in the manual.

    I don't have a complete game, but there is a number of example scenes included, they are listed here:
    http://hiphish.github.io/grid-framework/examples

    I am still trying to figure out a way to include interactive web player examples on the site, but if you want I can build an example for you and send you a link.
     
  33. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    Thanks for the quick reply. I don't need the samples. I just purchased the game. I am looking into the samples. Thanks for putting the new UI on the feature list.

    I am working on a puzzle game. There is one main panel for the puzzle and smaller child panels by the side for the score etc. Since I can't use the new 4.6 UI with anchors for the panels, I would like to use a simple Plane object as a Panel. How do I anchor the grid to the Plane, so it always sizes with Plane and keeps the same number of proportional columns no matter whether mobile or Ipad? I can size the Panel based on percentages, but I would need the grid to size accordingly. Is there a sample for this functionality?

    Syed
     
    Last edited: Jan 17, 2015
  34. kregenrek

    kregenrek

    Joined:
    Dec 21, 2014
    Posts:
    10
    Hi,

    I didn't follow the whole thread so excuse me if I ask twice.

    My first question is rather about game design, best practice.
    Could you give me a tip how i could do some collision detection in grid framework. I have no problem with static tiles, i just use 1,0 for passable and not passable tiles and store the Information in an array. Like in your example grid movement with walls.
    But what about moving enemies?
    The Position of the enemie is dynamic (follow player, random, etc.).

    Now the problem: every enemie is allowed to move one tile at a time. If enemy 1 wants from position <4,4> to <5,4> and enemy2 from <5,5> to <5,4> both check if <5,4> is free and of course its free at this time but if both enemies move now they collide.

    Actually i check this with raycasting but i dont know if it could be a better way. Maybe to store enemies in an array and loop through.

    Second qustion what if a object is at grid position <2,1> (in certain cases they arent aligned to the grid) and i want to move it to the nearest grid right on the axis.
    So the nearest grid position would be at:<1.5,1>.
    The problem: i always do
    Transform.position + transform.foward
    <2,1> + <-1, 0>

    So the new position would be <1,1> and
    Grid.nearestboxg() gives me now <0.5,1> and not <1.5,1>. I dont know why.

    I hope my questions are clear enough. I also could post some source code if you not sure about.


    Best regards
    Kevin
     
  35. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    What you are looking for is the size or renderFrom/renderTo of the grid and the spacing (I assume you are talking about rectangular grids). Make sure the grid is using relative size first. Then you make the spacing of the grid proportional to the scale of your object, that will make sure the number of cells in your grid stays the same and the grid resizes. For simplicity I have attached the grid component to the the object itself, that way they use the same Transform. Here is the complete script:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(GFRectGrid))]
    4. public class NewBehaviourScript : MonoBehaviour {
    5.     Vector3 intialRatio;
    6.     GFRectGrid grid;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         grid = GetComponent<GFRectGrid>();
    11.         intialRatio = new Vector3(
    12.                 grid.spacing.x / transform.lossyScale.x,
    13.                 grid.spacing.y / transform.lossyScale.y,
    14.                 grid.spacing.z / transform.lossyScale.z
    15.                 );
    16.         grid.relativeSize = true;
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         var spacing = new Vector3();
    22.         for (int i = 0; i < 3; ++i) {
    23.             spacing[i] = intialRatio[i] * transform.lossyScale[i];
    24.         }
    25.         grid.spacing = spacing;
    26.     }
    27. }
    Try it out, create an object, any object, even a cube is good enough, attach the script and a rectangular grid to it and run the scene. If you change the scale of your object the spacing of the grid will adapt. Instead of the scale you can use any other Vector3. It even works with the RectTransform of a canvas if the render mode is set to world-space.

    How about instead of only storing whether a square is occupied you store more information: who is occupying it and who wants to occupy it. During every step the actors will try to reserve a tile for themselves and it everyone gets one tile it's done. However, if two actors want to reserve the same tile you need to resolve the conflict.

    I would look at real life for a solution: every agent has a priority rating, lower-priority agents will let higher priority ones pass and pick another tile instead. If two agents have the same priority pick one at random. You could even change priority dynamically: for example normally worker agents would have a higher priority than fighting agents, but while under attack fighting agents would have a higher priority.

    There is one issue though: many high priority agents could block the path forever and get the lower ones stuck. I would add a second rating for impatience: every time an agent lets another one pass increase its impatience, every time it gets a pass lower it. The higher the priority of the agent you let pass, the less the impatience of the waiting on should rise. When you're in the car you are more tolerant towards an ambulance (high priority) than towards a bus (low priority). That way low-priority agents will get a pass eventually.

    That's at least how I would handle it, but I'm sure there are many other solutions. I'm not an expert on these issues.

    The thing is, if you're exactly between two points there is no nearest point. The solution is to either crash because the term "nearest" does not make any sense or just pick one arbitrarily. It's like asking whether 0 is a positive or negative number. I decided to pick one arbitrarily, so the behaviour can be considered undefined (meaning no one knows what will happen). I'm afraid there is no solution to this dilemma; you could add a little bias to your position, so if you want to move north add (0.1, 0, 0) to the position before getting the nearest box.

    Keep always im mind that floating point arithmetic is inherently imprecise and even when the computer is showing you 1 it does not necessarily mean exactly 1. If you are not familiar with floating point arithmetic, here is a good video explaining the problem:
     
  36. kregenrek

    kregenrek

    Joined:
    Dec 21, 2014
    Posts:
    10
    That makes sense pick the higher priority randomly and let this one pass. But I'm scratching my head how to resolve this. Normally I would use add to every enemy a Script "Enemy.cs" and add a method OnCollisionEnter with something like this:

    Code (CSharp):
    1.        void OnCollisionEnter(Collision target)
    2.          {
    3.              if(target.gameObject.name == "enemy2")
    4.              {
    5.                  var enemy2 = target.gameObject.GetComponent<Enemy>();
    6.                  // first stop movement of enemy2 immediatly
    7.                  enemy2.StopMovement();
    8.                
    9.                  //  rotate the enemy2 to the enemy1 direction
    10.                  enemy.transform.rotation = transform.rotation;
    11.                  // move enemy2 to the next tile
    12.                  enemy.MoveStraight(enemy.transform.position + enemy.transform.forward);
    13.                
    14.                  // now do enemy1 logic (finish movement and wait 1 second on the new position)
    15.                  // then continue random movement
    16.              }
    17.          }

    But this example is too static (what about enemy 3-x). When i change to if(target.gameObject.tag == "enemy") the problem is that OnCollisionEnter fires twice - each for every enemy so that i can't do the special behaviour on enemy1 and enemy2.

    Nevermind I think that there is a simpler way to do it. For example - here is the perfect example what I want achieve: (Look at the two blue enemies which collide)



    Thank you for your thoughts. I ended up with this:

    Code (CSharp):
    1.  
    2. // if there is a collision divide the forward / 2 (because in my case collision is always hit in the middle 2.0, 3.0, 4.0)
    3. enemy.cachedTransform.rotation = cachedTransform.rotation;
    4. enemy.MoveStraight(grid.NearestBoxG(enemy.cachedTransform.position + enemy.cachedTransform.forward / 2));
    5.  
    Thank you!

    Regards
    Kevin
     
  37. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Why even use collision at all? If two agents want to step on the same tile you know they will collide and if they are moving are the same speed you also know when they will collide: halfway through the movement. The winning agent performs its animation the usual way, but the losing agent is assigned a different animation to play: move half a tile forward and half a tile backwards.

    This is where I would use the Delegation Pattern. There is one main script, the delegate, that decides what to do, while many minor scripts, the delegators, answer to the delegate. The delegate would keep track of the scene and the grid, while your agents would be the delegators. The delegate keeps track of what tiles are occupied or free and the delegators tell the delegate "I would like to step on that tile". The delegate then resolves any conflicts and tells the delegators what they are supposed to do, i.e. assign target positions and animations.

    If two agents want to step onto the same tile the delegate can then look at them, determine their priority and impatience and then decide which one is supposed to walk where. If the delegate sees that two would collide it tells one of them to play the back-and-forth animation and the other to play the regular walking animation. Then it would look like one of them is pushing the other.
     
  38. hiphish

    hiphish

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

    Version 1.7.2 of Grid Framework has been approved by the Asset Store team. This release and the previous one provide bugfixes:
    • Fixed: Null exception on polar grids when getting Vectrosity points if the grid is not being rendered.

    For version 1.7.1:
    • Fixed: The grid align panel now correctly respect or ignores rotation when auto-snapping.
     
  39. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Playable examples

    I have added playable builds of the examples to the website, now you can click the title of an example and try the result out in the Web Player. To get back click on the Grid Framework examples label above the player. Just remember that you have to click into the player to get it to accept input.

    Check out the web pages:
    http://hiphish.github.io/grid-framework/examples/
     
  40. smilejp

    smilejp

    Joined:
    Jan 18, 2013
    Posts:
    2
    Hi hiphish,

    i have some question about Render Grid.

    i''m using RectGrid, set size 150x150 and axis color (94,94,94,64).

    play on iOS and Android, first time, axis color show correct display.
    when i moved camera, axis alpha color shows incorrect.
    It seems like the alpha value is set to 1.



    tried a few things to fix the problem.
    first. find RenderGridLines() function,
    tested Debug.Log(GL.Color(colors)) but color value is fine.

    second. change default shader code,
    did not solved the problem.

    I do not know how to fix this.
    have you good idea?

    Thank you.
     

    Attached Files:

  41. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Unity has had problems with lit shaders on mobile platforms in the past, looks like it's still the case. You should try an unlit shader, there are some on this site:
    http://owlchemylabs.com/content/
     
  42. smilejp

    smilejp

    Joined:
    Jan 18, 2013
    Posts:
    2
    Thanks for the quick reply.
    solved the problem using AlphaSelfIllum + alpha blend shader.

    Thank you.
     
  43. dc.

    dc.

    Joined:
    Mar 22, 2014
    Posts:
    3
    I have a small uissue.

    when using the example "Runtime Snapping" and setting the GFRect Grid to a spacing of 10 and resizing cube_large the Y axis is out by 5, see the screenshots
     

    Attached Files:

  44. dc.

    dc.

    Joined:
    Mar 22, 2014
    Posts:
    3
    I have found the issue, Spacing y has to be set to 1 while both X and Z set to 10.
     
  45. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Yes, the method in question is the following:
    Code (csharp):
    1. Vector3 CalculateOffsetY(){
    2.     //first store the objects position in grid coordinates
    3.     var gridPosition = grid.WorldToGrid(transform.position);
    4.     //then change only the Y coordinate
    5.     gridPosition.y = 0.5f * transform.lossyScale.y;
    6.    
    7.     //convert the result back to world coordinates
    8.     return grid.GridToWorld(gridPosition);
    9. }
    If your object has a lossy scale (i.e. world scale) of let's say 5, then it means the centre of the object has to be 2.5 world units above the grid. If the spacing of the grid is not 1, then it will scale the resulting world position accordingly. That means if the scaling is 2, the object will be 2 * 2.5 = 5 world units above the grid. To fix this we can divide by the spacing of the grid:
    Code (csharp):
    1. gridPosition.y = 0.5f * transform.lossyScale.y / grid.spacing.y
    This only works for rectangular grids because only they have a spacing. For layered grids you have to use the depth.
     
  46. SuHwanK

    SuHwanK

    Joined:
    Dec 30, 2014
    Posts:
    43
    Hi,
    i'm bought it. It's very nice.
    but i don't found document(manual) about this plug-in(Grid Framework).
    so i need it(document).
    and i have one question.
    How can i set Spacing to Draw Grid Line.(Another Color)
    For Example. green line, green line, red line, green line, green line, red line...
    thank you.
     
  47. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi, the documentation is under Help -> Grid Framework Documentation in Unity, it's in HTML format. Colours can only be set per axis, but you cannot alternate them on an individual axis. You can get that effect by using the Vectrocity extension:
    http://hiphish.github.io/grid-framework/examples/vectrosity/
    The upper left grid is alternating its colours for each line individually. Grid Framework has a method that will return you the vertices in a format that's ready for Vectrocity, but beyond that it is outside my domain.
    http://starscenesoftware.com/vectrosity.html
    As a disclaimer, I am in no way affiliated with the author of Vectrosity.
     
  48. xm_zq

    xm_zq

    Joined:
    Feb 4, 2015
    Posts:
    2
    Hello , I have a problem now , art staff gave me a model , not the same height and width . When adsorbed to the surface of the grid , arranged the model does not fill the specified number of grids , such as my model in the early modeling, there are provisions that it occupies an area of 4 * 2 grid , in the arrangement adsorbed to network Georgia will be offset not aligned.Vacant, no adsorbed to the surface of the grid
    Thank you.
    Code (CSharp):
    1. _grid.AlignTransform(_cachedTransform);
    2. _cachedTransform.position = CalculateOffsetY();
     
  49. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hello; I need more information than that: what is the scale of the object in the scene, what are the settings of the grid, what is the expected outcome. For example, the mesh of your model might be 2 x 3 world units large, but if the scale of the GameObject is 1 x 1 it will be treated like a 1 x 1object. The scale is the only reliable universal way of telling how "large" an object in Unit is.
     
  50. xm_zq

    xm_zq

    Joined:
    Feb 4, 2015
    Posts:
    2
    Hey hiphish. My current project is the case. Rect Grid is the size of 10 x and z,x and z are 0.5 spacing, so generating a mesh 20*20, art staff is also in 3dmax reference grid size 20*20 in production. Regulate the placement model currently occupied several grid squares. When I put unity to it addsorbeb onto the grid when the position offset,they are not aligned,and some models will leave the mesh surface.I think it is not concerned with this.
    Code (CSharp):
    1. _gridPosition.y = 0.5f * _cachedTransform.lossyScale.y;