void Start() { if (resourceGrid == null) { resourceGrid = GameObject.FindGameObjectWithTag("Map").GetComponent <ResourceGrid> (); } if (resourceGrid != null) { // Get Paths to capital from all Spawn Positions for (int x = 0; x < spawnPositions.Length; x++) { resourceGrid.GenerateWalkPath(resourceGrid.capitalSpawnX, resourceGrid.capitalSpawnY, false, (int)spawnPositions [x].x, (int)spawnPositions [x].y); if (resourceGrid.pathForEnemy != null) { FillPath(resourceGrid.pathForEnemy, x, false); } } // Then get Paths to kamikaze destinations for (int x = 0; x < spawnPositions.Length; x++) { resourceGrid.GenerateWalkPath((int)kamikazeDestinations[x].x, (int)kamikazeDestinations[x].y, false, (int)spawnPositions [x].x, (int)spawnPositions [x].y); if (resourceGrid.pathForEnemy != null) { FillPath(resourceGrid.pathForEnemy, x, true); } } } }
void Update() { // I need to change this later to the script that handles all Player clicking/controls, // instead of here - a script that should only control a selected Unit's movement. if (isSelected && Input.GetMouseButtonDown(1)) { // If this unit is selected and Player left clicks // Store the mouse position in world coords Vector3 m = Camera.main.ScreenToWorldPoint(Input.mousePosition); mX = Mathf.RoundToInt(m.x); mY = Mathf.RoundToInt(m.y); // Making sure that we are not trying to path outside the BOUNDARIES of the GRID if (mX > resourceGrid.mapSizeX - 1) { mX = resourceGrid.mapSizeX - 1; } if (mY > resourceGrid.mapSizeY - 1) { mY = resourceGrid.mapSizeY - 1; } if (mX < 0) { mX = 0; } if (mY < 0) { mY = 0; } // give resource grid this gameobject as unit to path resourceGrid.unitOnPath = gameObject; // and generate a path using mouse coords ROUNDED as int resourceGrid.GenerateWalkPath(mX, mY, true, posX, posY); // THIS WILL NOT WORK if ACTUAL tiles BOTTOM LEFT is NOT 0.0!! moving = true; isSelected = false; } if (currentPath != null) { int currNode = 0; while (currNode < currentPath.Count - 1) { Vector3 start = resourceGrid.TileCoordToWorldCoord(currentPath [currNode].x, currentPath [currNode].y); Vector3 end = resourceGrid.TileCoordToWorldCoord(currentPath [currNode + 1].x, currentPath [currNode + 1].y); ; Debug.DrawLine(start, end, Color.red); currNode++; } } if (moving) { // Have we moved close enough to the target tile that we can move to next tile in current path? if (Vector2.Distance(transform.position, resourceGrid.TileCoordToWorldCoord(posX, posY)) < 1f) { MoveToNextTile(); } if (Vector2.Distance(transform.position, resourceGrid.TileCoordToWorldCoord(mX, mY)) < 0.1f) { movingToAttack = false; moving = false; anim.SetTrigger("idle"); anim.ResetTrigger("movingRight"); anim.ResetTrigger("movingLeft"); anim.ResetTrigger("attackRight"); anim.ResetTrigger("attackLeft"); } else { if (mX > transform.position.x) { if (movingToAttack) { anim.SetTrigger("attackRight"); anim.ResetTrigger("movingRight"); anim.ResetTrigger("idle"); } else { anim.SetTrigger("movingRight"); anim.ResetTrigger("idle"); anim.ResetTrigger("movingLeft"); anim.ResetTrigger("attackRight"); } } if (mX < transform.position.x) { if (movingToAttack) { anim.SetTrigger("attackLeft"); anim.ResetTrigger("movingLeft"); anim.ResetTrigger("idle"); } else { anim.SetTrigger("movingLeft"); anim.ResetTrigger("idle"); anim.ResetTrigger("movingRight"); anim.ResetTrigger("attackLeft"); } } // DO ANIMATION CALCULATION HERE: // anim.SetTrigger ("moving"); // anim.ResetTrigger("idle"); } if (movingToAttack) { transform.position = Vector2.MoveTowards(transform.position, new Vector2(mX, mY), movementSpeed * Time.deltaTime); } else { // This MOVEMENT will animate towards the next position transform.position = Vector2.MoveTowards(transform.position, resourceGrid.TileCoordToWorldCoord(posX, posY), movementSpeed * Time.deltaTime); } } }