// ------------------------------------------------------------------------------------------------------------ #region update/ input protected void Update() { if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0 && unitMoving == false) { // Check what the player clicked on. I've set Tiles to be on Layer 8 and Unit on Layer 9. // Each tile has its own collider attached but I could have used a big invisible collider // that stretches the whole map as a catch all for clicks that did not fall on a unit. // The only reason the tiles each have a collider is because they are used in other // samples too that requires it. RaycastHit hit; Ray r = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(r, out hit, Mathf.Infinity, clickMask)) { if (hit.transform.gameObject.layer == 8) { // a tile was clicked Sample4Tile n = map.NodeAtWorldPosition <Sample4Tile>(hit.point); //Sample4Tile n = (Sample4Tile)map.NodeAtWorldPosition(hit.point); if (n != null) { Debug.Log("Clicked on: " + n.tileName); // check if a tile that the active unit may move to if (activeUnit != null && validMoveNodes.Contains(n)) { // get the path List <MapNavNode> path = map.Path <MapNavNode>(activeUnit.tile, n, OnNodeCostCallback); if (path != null) { unitMoving = true; // need to wait while unit is moving ClearMoveMarkers(); activeUnit.Move(path, OnUnitMoveComplete); } } } } else { // a unit was clicked. first clear the previously selected unit if (activeUnit != null) { activeUnit.UnitDeSelected(); ClearMoveMarkers(); } activeUnit = hit.transform.GetComponent <Sample4Unit>(); if (activeUnit != null) { // tell the unit it was selected so that it can change colour activeUnit.UnitSelected(); // show the player the area that the unit may move to UpdateMoveMarkers(); } } } } }
protected bool unitMoving = false; // set when a unit is moving and no input should be accepted #endregion // ------------------------------------------------------------------------------------------------------------ #region start protected void Start() { // get reference to the mapnav map map = GameObject.Find("Map").GetComponent <Sample4Map>(); // also used in sample4b if (map == null) { map = GameObject.Find("Map").GetComponent <Sample4bMap>(); } // in this sample I will simply spawn 10 units on random locations of the map for (int i = 0; i < 10; i++) { Sample4Tile selectedTile = null; // find a tile to place the unit on. I will randomly select one. // but I need to check if there is not already a unit on the // selected tile before accepting it. while (true) { int idx = Random.Range(0, map.grid.Length); // make sure this is a valid node if (map.ValidIDX(idx)) { // now check if there is not already a unit on it selectedTile = map.grid[idx] as Sample4Tile; if (selectedTile.unit == null) { // no unit on it, lets use it break; } } } // create the unit and place it on the tile GameObject go = (GameObject)Instantiate(unitFab); go.transform.position = selectedTile.position; go.layer = 9; // in this sample Units must be in layer 9 // be sure to tell the tile that this Unit is on it Sample4Unit unit = go.GetComponent <Sample4Unit>(); unit.Resetunit(); // reset its moves now too unit.LinkWithTile(selectedTile); units.Add(unit); // keep a list of all units for quick access } }