Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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

alt text

not-quite-what-i-wanted.png (170.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
             
         }
     
 }
 }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Grid Based Question 0 Answers

Programming an isometric (top down) grid 0 Answers

Help optimizing my code for chess game 1 Answer

Tileset, large maps and game object 0 Answers

How do you find adjacent tiles of the same type in grid array [x,y].type == [x+1,y].type 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges