void Build() { GhostBuilding gb = currentObject.GetComponent <GhostBuilding>(); if (!gb.IsTriggered() && Input.GetMouseButtonDown(0)) { if (currentObject == wallGhost) { if (goldPile.getGold() > wallCost) { goldPile.deductGold(wallCost); GameObject wall = (GameObject)Instantiate(wallGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (wallCost + 1)); } } else if (currentObject == gateGhost) { if (goldPile.getGold() > gateCost) { goldPile.deductGold(gateCost); GameObject gate = (GameObject)Instantiate(gateGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (gateCost + 1)); } } else if (currentObject == towerGhost) { if (goldPile.getGold() > towerCost) { goldPile.deductGold(towerCost); GameObject tower = (GameObject)Instantiate(towerGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (towerCost + 1)); } } else if (currentObject == barracksGhost) { if (goldPile.getGold() > barracksCost) { goldPile.deductGold(barracksCost); GameObject barracks = (GameObject)Instantiate(barracksGhostPrefab, currentObject.transform.position, currentObject.transform.rotation); } else { // NOT ENOUGH MINERALS Debug.Log("NOT ENOUGH MINERALS: " + goldPile.getGold() + ", Need: " + (barracksCost + 1)); } } } }
void ShowGhost() { int layerMask = 1 << 9; layerMask = ~layerMask; // Set color GhostBuilding gb = currentObject.GetComponent <GhostBuilding>(); if (gb.IsTriggered() || GetCost(currentObject) > goldPile.getGold()) { wallGhost.GetComponent <Renderer>().material = failBuildMat; gateGhost.GetComponent <Renderer>().material = failBuildMat; Renderer[] rends = towerGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = failBuildMat; } rend.materials = mats; } rends = barracksGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = failBuildMat; } rend.materials = mats; } } else { wallGhost.GetComponent <Renderer>().material = correctBuildMat; gateGhost.GetComponent <Renderer>().material = correctBuildMat; Renderer[] rends = towerGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = correctBuildMat; } rend.materials = mats; } rends = barracksGhost.GetComponentsInChildren <Renderer>(); foreach (Renderer rend in rends) { Material[] mats = rend.materials; for (int i = 0; i < mats.Length; i++) { mats [i] = correctBuildMat; } rend.materials = mats; } } RaycastHit hit; Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) { if (currentObject == barracksGhost) { currentObject.transform.position = new Vector3(hit.point.x, hit.point.y - 0.5f, hit.point.z); } else { currentObject.transform.position = hit.point; } currentObject.transform.position = new Vector3(currentObject.transform.position.x, currentObject.transform.position.y + 0.5f, currentObject.transform.position.z); } }