Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Raspilicious · Jun 11, 2012 at 06:01 PM · pathfindingselectionturn-basedselecthexagon

Hexagon A* Pathfinding - limiting path length

Hi all.

I've been working further with Aron Granberg's A* pathfinding, using List Graphs to create paths between nodes on a hexagon grid, for a turn-based game. What I am attempting to do now though, is to limit the number of nodes to be traversed by a particular unit.

At the moment I have working pathfinding, and a few scripts that are supposed to limit the path length. There are two game objects, a blue unit and a red unit, each with the same script for 'movement range' (but red having 2 range and blue having 6 range). When either unit is selected, it enables me to set a path, and the unit stops at its maximum range, even if the path itself is longer. The issue arises when I choose the red unit and set a path longer than it's maximum movement range. It moves 2 hexes and stops, which is correct. But then if I then select the blue unit, the red unit decides "hey now I have movement of 6!" and proceeds to move the other 4 units to end up having moved 6 hexes from it's original hex.

I think that the solution involves creating instances of the units, so that each time the pathfinding script is called it creates it's own little world, and can't be interacted with by a second pathfinding script call... but I don't know how to do that, or if it's even the right thing to do.

Here are my scripts so far.

First, the unit select and start-the-path script (which also changes the material at the very end, for 1 second):

 public class aaTouchRaycaster : MonoBehaviour {
     public Ray ray;
     public RaycastHit hit;
     public Vector3 targetPosition;
     public AstarAI aStarAIScript;
     public aaManager aaManagerScript;
     public aaCountdowns aaCountdownsScript;
 
     void Update () {
         int i = 0;
         while (i < Input.touchCount) {
             if (Input.GetTouch(i).phase == TouchPhase.Began) {
                 ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
 
                 //if selectedUnit = null, do nothing
                 if (Physics.Raycast(ray, out hit, 100) && hit.collider.gameObject.tag == "HexTile" && aaManagerScript.selectedUnit == null)
                     Debug.Log("there is no unit selected!");
 
                 //if selectedUnit = something, send it on it's path to the targetPosition node
                 if (Physics.Raycast(ray, out hit, 100) && hit.collider.gameObject.tag == "HexTile" && aaManagerScript.selectedUnit != null) {
                     Node node = AstarPath.active.GetNearest (aaManagerScript.selectedUnit.transform.position);
                     targetPosition = AstarPath.active.GetNearest (hit.point);
                     if (node.walkable)
                         aaManagerScript.selectedUnit.GetComponent<AstarAI>().MoveOnThePath();
                 }
 
                 //selects a unit
                 //ie: sets the game object hit by the Raycast as the selected 'unit' in the aaManagerScript.selectedUnit GameObject variable
                 if (Physics.Raycast(ray, out hit, 100) && hit.collider.gameObject.layer == 9) {
                     aaManagerScript.selectedUnit = hit.collider.gameObject;
                     aaManagerScript.origMaterial = aaManagerScript.selectedUnit.renderer.material;
                     aaManagerScript.selectedUnit.renderer.material = aaManagerScript.newMaterial;
                     aaCountdownsScript.matCountdown = true;
                     aaCountdownsScript.matResetTimer = 1.0f;
                 }
             }
             ++i;
         }
     }
 }

And secondly, the A* script, which I have added in my attempt at restricting movement range:

 public class AstarAI : MonoBehaviour {
     public aaTouchRaycaster rayScript;
     public aaManager aaManagerScript;
     public GameObject targetNode;
     private Seeker seeker;
     private CharacterController controller;
     //the calculated path
     public Path path;
     //the AI's speed per second
     public float speed = 100;
     //the max distance from the AI to a waypoint for it to continue to the next waypoint
     public float nextWaypointDistance = 1;
     //the waypoint we are currently moving towards
     private int currentWaypoint = 0;
     public bool nodeIsClose = false;
     public float countdownResetValue = 2.0f;
     public float countdown = 2.0f;
 
     public void Start () {
         controller = GetComponent<CharacterController>();
     }
 
     public void MyCompleteFunction (Path p) {
         if (!p.error) {
             path = p;
             //reset the waypoint counter
             currentWaypoint = 0;
         }
     }

     public void Update () {
         if(path == null)
             return;
         if(currentWaypoint >= path.vectorPath.Length)
             return;
         Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position.normalized;
         dir *= speed * Time.deltaTime;
         controller.SimpleMove(dir);
         if(Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance){
             nodeIsClose = true;
         }else{
             nodeIsClose = false;
             return;
         }
         if(nodeIsClose)
             countdown -= Time.deltaTime;
         if(nodeIsClose && countdown <= 0.0f){
             if(currentWaypoint <= aaManagerScript.selectedUnit.GetComponent<UnitStats>().uMoveRange - 1){
                 currentWaypoint++;
                 countdown = countdownResetValue;
             }
             if(currentWaypoint == aaManagerScript.selectedUnit.GetComponent<UnitStats>().uMoveRange - 1)
                 Debug.Log("final node set");
         }
     }
 }

The reason for the -1 in the uMoveRange is that the first waypoint is registered as 0 (zero), the second waypoint as 1, etc, as opposed to the unit's movement range starting from 1, not 0.

TLDR; I don't know how to get a working movement range using pathfinding with multiple units.

Thanks in advance to whomever can help me out with this predicament. :)

Comment
Add comment · Show 1
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
avatar image Raspilicious · Jun 19, 2012 at 09:36 AM 0
Share

Bump, can anybody help me with this?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I get my pathfinding algorithm to know that it cant go through certain sides of tiles 1 Answer

3D Grid Based Movement (X-Com 2012 etc) – How to implement vertical movement? 0 Answers

Selecting custom game objects in Scene view window. 1 Answer

Object Selection does not stop on next object 0 Answers

Help with storing and recalling choices 1 Answer


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