- Home /
Question by
J-Dasse · Apr 02, 2018 at 06:45 PM ·
gridtilerangeturn-based
How can I highlight tiles that are in Range of attack?
I'm trying to make a turn based tactics game and since a noob in Unity I decided to start just checking some aspects of it
I'm stuck in the issue of attack range
1st_ I use a trigger sphere to tell me if the what tiles are kinda in range and store them in a list 2nd_ then I run a check to see if they are really in between range 3rd_ the tiles in range change colour
The second part just does not work either ALL tiles in the list change color or I just get some weird patterns
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class AttackRange : MonoBehaviour {
public int attackRange;
float distancex;
float distancez;
float tileDistance;
List<GameObject> inRange = new List<GameObject>();
void Start ()
{
this.transform.localScale = new Vector3 ((float)attackRange, (float)attackRange, (float)attackRange);
}
private void OnTriggerEnter(Collider other)
{
//detect all objects inside the RangeSphere
if ((!inRange.Contains(other.gameObject) && (other.tag == "Tile")))
{
//If it's a Tile add it to the list
inRange.Add (other.gameObject);
}
//Sort through the list
foreach (GameObject tile in inRange)
{
//Get the x and z components of the distance
//between the Tile and the Center of the RangeSphere
distancex = tile.transform.position.x - this.transform.position.x;
distancez = tile.transform.position.z - this.transform.position.z;
//Get the Sum of the x and z components of the distance to get a Tile based distance
tileDistance = distancex + distancez;
//chek if the Tile based distance is in Range
//and Mark it as inRange
if (tileDistance <= attackRange)
{
tile.GetComponent<TileStatus> ().inAttackRange = true;
}
}
}
}
current result
not-quite-what-i-wanted.png
(170.1 kB)
Comment
Answer by J-Dasse · Apr 02, 2018 at 09:28 PM
So I came up with a not very elegant solution but it works
public class AttackRange : MonoBehaviour {
public int attackRange;
float distancex;
float distancez;
float tileDistance;
List<GameObject> inRange = new List<GameObject>();
void Start ()
{
this.transform.localScale = new Vector3 ((float)attackRange, (float)attackRange, (float)attackRange);
}
private void OnTriggerEnter(Collider other)
{
//detect all objects inside the RangeSphere
if ((!inRange.Contains(other.gameObject) && (other.tag == "Tile")))
{
//If is a Tile add it to the list
inRange.Add (other.gameObject);
}
//Sort through the list
foreach (GameObject tile in inRange)
{
//Get the x and z components of the distance
//between the Tile and the Center of the RangeSphere
//Not very elegant solution to check quadrants
//Should use Switch?
if (tile.transform.position.x > this.transform.position.x)
{
distancex = (tile.transform.position.x) - (this.transform.position.x);
}
if (tile.transform.position.x <= this.transform.position.x)
{
distancex = (this.transform.position.x) - (tile.transform.position.x);
}
if (tile.transform.position.z > this.transform.position.z)
{
distancez = (tile.transform.position.z) - (this.transform.position.z);
}
if (tile.transform.position.z <= this.transform.position.z)
{
distancez = (this.transform.position.z) - (tile.transform.position.z);
}
//Get the Sum of the x and z components of the distance to get a Tile based distance
tileDistance = distancex + distancez;
//chek if the Tile based distance is in Range
//and Mark it as inRange
if (-attackRange <= tileDistance && tileDistance<= attackRange)
{
tile.GetComponent<TileStatus> ().inAttackRange = true;
}
}
}
}