3D Tile Navigation and Storage : Documentation
How to set up the plugin:
Import the entire asset folder into your project.
Levelprefab into your scene.
It will show up as an empty GameObject, as levels are loaded at run time.
Position the camera and set up lights.
You can run the game now, and the level should appear. Press spacebar to toggle levels, and
Zto 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:
Drag in the
EditorHexLevelprefabs into the scene. These editors are basically GameObjects with tiles, buildings and units attached as children. The editor objects also have the
LevelWriter.csscript attached to them.
Try moving around the tiles. Tip: Hold the
Ctrlkey while moving to snap to increments (works for square tiles only, since hex tiles don't snap to increments of one on both axes).
Run the game to see the changes reflected in the actual level.
How this works (an overview):
Level.cs class to understand how to interact with this system. Here's what happens in that class:
Level.cs script calls
LevelReader reads from the level data files located in Resources/Levels/....txt. It then instantiates new prefabs of
Units from the read level data.
Level.cs scripts calls the
Graph.cs class to generate a traversal graph. The traversal graph determines how movement is possible across different tiles.
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 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.
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.
Core folder includes all of the classes you'll want to inherit:
IO folder contains
LevelWriter classes, which are used to read from and write levels to files.
Pathfinding folder contains pathfinding and path visualization scripts.
Tiles folders contain classes which inherit the respective core classes. You'll want to add more classes here.
This is an overview of the important classes and the public methods they offer:
// 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)
// 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)
Inherits Tile and overrides
// 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)
// Public variables public string Name; public Tile MainTile; // Public methods // Initializer public virtual void Initialize(Tile mainTile)