Search Unity

(novice) Confused by an error in this implementation of The A* Project:

Discussion in 'Navigation' started by AidanofVT, May 26, 2020.

  1. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    It's a top-down RTS-style interface in a 2d environment. I'm trying to get it so that when the background (represented by a quad with a mesh collider) gets clicked on, the selected units move to the cursor location, while if a unit gets clicked on, the selected units move to the location of the clicked unit:

    From the unit script:
    Code (CSharp):
    1.     void OnMouseOver() {
    2.         if (Input.GetKeyDown(KeyCode.Mouse0)) {
    3.             Debug.Log("Click Registered.");
    4.             gameState.addActiveUnit(gameObject);
    5.             highlight();
    6.         }
    7.         else if (Input.GetKeyDown(KeyCode.Mouse1)) {
    8.             Debug.Log("Right clicked.");
    9.             driver.thingRightClicked(gameObject);
    10.             Debug.Log("Called Driver");
    11.         }
    12.     }
    From the ground script:
    Code (CSharp):
    1.     private void OnMouseOver() {
    2.         if (Input.GetKeyDown(KeyCode.Mouse0)) {
    3.             Debug.Log("Click Registered.");
    4.             driver.clearActiveUnits();
    5.         }
    6.         else if (Input.GetKeyDown(KeyCode.Mouse1)) {
    7.             Debug.Log("Right clicked.");
    8.             driver.thingRightClicked(gameObject);
    9.             Debug.Log("Called Driver");
    10.         }
    11.     }
    From the driver script:
    Code (CSharp):
    1.     public void thingRightClicked (GameObject thingClicked) {
    2.         Vector3 destination;
    3.         switch (thingClicked.tag) {
    4.             case "unit":
    5.                 destination = thingClicked.transform.position;
    6.                 break;
    7.             case "ground":
    8.                 destination = Input.mousePosition;
    9.                 break;
    10.             default:
    11.                 destination = new Vector3(0,0,0);
    12.                 break;
    13.         }
    14.         foreach (GameObject unit in gameState.getActiveUnits()) {
    15.             unit.GetComponent<Unit>().move(destination);
    16.             Debug.Log("Called unit.move");
    17.         }
    18.     }
    From the movement script in the Unit game-object.
    Code (CSharp):
    1. void Start() {
    2.         seeker = GetComponent<Seeker>();
    3.     }
    4.  
    5.     public void setDestination (Vector3 destination) {
    6.         seeker.StartPath(transform.position, destination, OnPathComplete);
    7.         Debug.Log("Destination set.");
    8.     }
    9.  
    10.     void OnPathComplete (Path finishedPath) {
    11.         Debug.Log("Yay, we got a path back. Did it have an error? " + finishedPath.error);
    12.         path = (ABPath) finishedPath;
    13.     }
    The program only malfunctions when the ground is clicked on: when the seeker.Startpath method gets called with the mouse location as a parameter, it does return a path, but the path has zero points in its vectorPath list. An ArgumentOutOfRangeException occurs soon thereafter.

    I'm stumped! Any insight would be appreciated.
     
  2. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    (Solved: I wasn't converting screen coordinates to world coordinates.)