- Home /
Detecting Object On Tile
I have a turn based strategy game for mobile, and the movement is based on tiles. I have a system setup so that it shows which tiles can be moved to, but I want a method to say which tiles you can attack, and I want to mark it by setting a sprite to active. To accomplish this the way I though of, I need to know which soldier is on each tile, if any. So how would I do that. Important scripts attached:
Movement: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class MovementController : MonoBehaviour
{
public int moveRadius;
public GameObject[] tilesInGame;
public List<GameObject> tilesToMove = new List<GameObject> ();
public List<GameObject> tilesToAttack = new List<GameObject> ();
public GameObject moveTarget;
public bool canMove;
private DefaultMenuManager defaultMenu;
void Start()
{
defaultMenu = GameObject.FindGameObjectWithTag ("GameManager").GetComponent<DefaultMenuManager> ();
tilesInGame = GameObject.FindGameObjectsWithTag ("Node");
canMove = true;
}
void Update()
{
}
//Update Targets for Movement
public void MoveTarget()
{
DisableAllTiles ();
AssignTilesWithinMove ();
ShowTarget ();
}
void DisableAllTiles()
{
foreach (GameObject tile in tilesInGame) {
tile.GetComponent<Node> ().targetMarker.SetActive (false);
tile.GetComponent<Node> ().canMoveTo = false;
tilesToMove.Remove (tile);
}
}
void AssignTilesWithinMove()
{
foreach (GameObject tile in tilesInGame) {
if (Vector3.Distance (transform.position, tile.transform.position) < moveRadius) {
tilesToMove.Add (tile);
}
}
}
void ShowTarget()
{
foreach (GameObject tile in tilesToMove) {
tile.GetComponent<Node> ().targetMarker.SetActive (true);
tile.GetComponent<Node> ().canMoveTo = true;
}
}
//Update Attack Markers
void AttackMarkers()
{
AssignTilesWithAttack ();
}
void AssignTilesWithAttack()
{
foreach (GameObject tile in tilesInGame) {
}
}
//Move Soldier
public void MoveSoldier(GameObject tile)
{
MovePosition (tile);
DisableAllTiles ();
}
void MovePosition(GameObject location)
{
Vector3 offset = new Vector3 (0, 0.375f, 0);
transform.position = location.transform.position + offset;
canMove = false;
}
}
Node:
using UnityEngine;
public class Node : MonoBehaviour
{
public enum TileType
{
GrassField, ShallowWater, DeepWater
}
public GameObject targetMarker;
public GameObject soldierOnTop;
public TileType tileType;
public bool canMoveTo;
void Start()
{
soldierOnTop = null;
targetMarker.SetActive (false);
canMoveTo = false;
}
}
Comment