I’m trying to make a game in which you direct an object towards a goal by creating a path. This path will be created on a grid by clicking adjacent tiles and toggling through left, right and forward in order to get the object to it’s destination.
Right now, it looks like this:
I’ve figured out translating the player from one point to another on the grid by clicking on the tiles.
However, I still need to figure out
-
How to limit grid movement so that only adjacent tiles can be selected.
-
How to toggle between textures on each tile by clicking multiple times in order to determine what direction the object faces. Say I click once on a tile, it should be a forward facing arrow, click again, it should be an arrow pointing to the right.
This is the code for the GameManager script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
public static GameManager instance;
public GameObject TilePrefab;
public GameObject UserPlayerPrefab;
public GameObject AIPlayerPrefab;
public int mapSize = 0;
List <List<Tile>> map = new List<List<Tile>>();
List <Player> players = new List<Player>();
int currentPlayerIndex = 0;
void Awake() {
instance = this;
}
// Use this for initialization
void Start () {
generateMap();
generatePlayers();
}
// Update is called once per frame
void Update () {
players[currentPlayerIndex].TurnUpdate();
}
public void nextTurn() {
if (currentPlayerIndex + 1 < players.Count) {
currentPlayerIndex++;
} else {
currentPlayerIndex = 0;
}
}
public void moveCurrentPlayer(Tile destTile) {
players[currentPlayerIndex].moveDestination = destTile.transform.position + 1.5f * Vector3.up;
}
void generateMap() {
map = new List<List<Tile>>();
for (int i = 0; i < mapSize; i++) {
List <Tile> row = new List<Tile>();
for (int j = 0; j < mapSize; j++) {
Tile tile = ((GameObject)Instantiate(TilePrefab, new Vector3(i - Mathf.Floor(mapSize/2),0, -j + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<Tile>();
tile.gridPosition = new Vector2(i, j);
row.Add (tile);
}
map.Add(row);
}
}
void generatePlayers() {
UserPlayer player;
player = ((GameObject)Instantiate(UserPlayerPrefab, new Vector3(0 - Mathf.Floor(mapSize/2),1.5f, -0 + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<UserPlayer>();
players.Add(player);
AIPlayer aiplayer = ((GameObject)Instantiate(AIPlayerPrefab, new Vector3(6 - Mathf.Floor(mapSize/2),1.5f, -4 + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<AIPlayer>();
players.Add(aiplayer);
}
}
This is the code for the tile pieces:
using UnityEngine;
using System.Collections;
public class Tile : MonoBehaviour {
public Vector2 gridPosition = Vector2.zero;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter() {
transform.renderer.material.color = Color.blue;
Debug.Log("my position is (" + gridPosition.x + "," + gridPosition.y);
}
void OnMouseExit() {
transform.renderer.material.color = Color.white;
}
void OnMouseDown() {
GameManager.instance.moveCurrentPlayer(this);
}
}
And this is the code for the player piece:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public Vector3 moveDestination;
public float moveSpeed = 10.0f;
void Awake () {
moveDestination = transform.position;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public virtual void TurnUpdate () {
}
}