Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

3D Tile Navigation and Storage : Documentation

How to set up the plugin:

  1. Import the entire asset folder into your project.

  2. Drag the Level prefab into your scene.

  3. It will show up as an empty GameObject, as levels are loaded at run time.

  4. Position the camera and set up lights.

  5. You can run the game now, and the level should appear. Press spacebar to toggle levels, and Z to toggle node visualization. Click on a tank and then click on a tile to make the tank move smoothly to that tile.

The above steps enable level reading, traversal graph generation and smooth unit movement. To set up level editing and writing to file:

  1. Drag in the EditorLevel and/or the EditorHexLevel prefabs into the scene. These editors are basically GameObjects with tiles, buildings and units attached as children. The editor objects also have the LevelWriter.cs script attached to them.

  2. Try moving around the tiles. Tip: Hold the Ctrl key while moving to snap to increments (works for square tiles only, since hex tiles don't snap to increments of one on both axes).

  3. Run the game to see the changes reflected in the actual level.

How this works (an overview):

Read the Level.cs class to understand how to interact with this system. Here's what happens in that class:

Level reading: The Level.cs script calls LevelReader.cs. The LevelReader reads from the level data files located in Resources/Levels/....txt. It then instantiates new prefabs of Tiles, Buildings and Units from the read level data.

Graph generation: Then, the Level.cs scripts calls the Graph.cs class to generate a traversal graph. The traversal graph determines how movement is possible across different tiles.

Movement: When you order a unit to move, the Graph class finds out the shortest path to that tile using the A* algorithm. This path is given to the Unit. The Unit then starts moving smoothly to the target location. Check out the Unit.cs class to see the movement implementation.

Scripts Folder Structure:

The scripts are present in the Scripts folder. They are further organized into different folders, by functionality.

The Level.cs script is present in the Scripts root. It is included in the project as an example. You'll probably want to delete this and call scripts from your own code.

The Core folder includes all of the classes you'll want to inherit: Building, Unit, Tile and HexTile.

The IO folder contains LevelReader and LevelWriter classes, which are used to read from and write levels to files.

The Pathfinding folder contains pathfinding and path visualization scripts.

The Buildings, Units and Tiles folders contain classes which inherit the respective core classes. You'll want to add more classes here.

API Documentation:

This is an overview of the important classes and the public methods they offer:

###Graph.cs

// Public variables
public List<Tile> Tiles;

// Public methods

// Dynamically generates a traversal graph from given tiles.
public static Graph GenerateGraphFromTiles(Tile[,,] allTiles, bool isHex)

// Returns the shortest path from source to destination.
public List<Tile> GetShortestPath(Tile from, Tile to)

// Gets tiles within the specified distance of the origin tile.
public List<Tile> GetTilesWithinDistance(Tile origin, int distance)

// Gets tiles within the specified traversal cost of the origin tile.
public List<Tile> GetTilesWithinCost(Tile origin, int cost)

// Turns the node and edge visualizers on or off.
public void SetVisualizer(bool isVisible)

// Turns the specified tiles' node visualizers green.
public void SetSelectedTiles(List<Tile> tilesToSelect)

###Tile.cs

// Public variables
public string Name;
public int X;
public int Y;
public int Z;
public List<Tile> Neighbours;
public bool AllowsTraversal = true; // Indicates whether this particular type of Tile allows traversal. You can set this value in classes inherited from the Tile class.
public int TraversalCost;
public bool IsTraversable; // Indicates whether the tile is traversable, or buried under other tiles in the level. This value is set by the Graph class automatically.

// Protected variables
protected Vector3 CentreUnitOffset; // Offset of the unit position when it is at the center of this tile.
protected Vector3 CentreUnitRotation; // Rotation of the unit position when it is at the center of this tile.
protected float[] ActualBoundaryHeights; // Heights of the boundaries used for the unit's traversal over the terrain. Should correspond to accurate world units.
protected float[] VirtualBoundaryHeights; // Heights of the boundaries used to determine if any two neighbouring tile boundary can be traversed. Check out the interaction between the SlopeTile and the BlockTile for details.

// Public methods

// Initializer
public abstract void Initialize();

// Computes the array coordinates from position.
public virtual int[] ArrayCoordinateFromPosition(Vector3 position)

// Returns the actual or virtual boundary height of this tile, with respect to the parameter tile.
public virtual float GetBoundaryHeightWithRespectTo(Tile tile, bool isVirtualHeight)

// Returns the position of a unit when it is standing on top of this tile.
public Vector3 GetUnitPosition()

// Returns the rotation of a unit when it is standing on top of this tile.
public Quaternion GetUnitRotation(Quaternion unitRotation)

// Turns the node and edge visualizers on or off.
public void SetVisualizer(bool isVisible)

// Turns the node visualizer green
public void SetSelected(bool isSelected)

###Hextile.cs

Inherits Tile and overrides ArrayCoordinateFromPosition() and GetBoundaryHeightWithRespectTo() methods.

###Unit.cs

// Public variables
public string Name;
public Tile Tile;

// Protected variables
protected static Level Level;
protected float Speed = 0.02f;
protected float AngularSpeed = 6.0f;

// Public methods

// Sets position and rotation according the tile
public void Initialize(Tile tile)

// Sets destination tile and calculates the waypoints
public virtual void TravelTo(Tile destinationTile, Graph graph)

###Building.cs

// Public variables
public string Name;
public Tile MainTile;

// Public methods

// Initializer
public virtual void Initialize(Tile mainTile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.