void Fire() { Vector3 direction = transform.right; Random.InitState((int)(Time.unscaledTime * 1000)); direction.x += Random.Range(-accuracyOffset, accuracyOffset); Random.InitState((int)(Time.unscaledTime * 2000)); direction.y += Random.Range(-accuracyOffset, accuracyOffset); Debug.Log(direction); RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 50.0f, layerMask); if (hit) { BuildingCollision shot = hit.collider.GetComponent <BuildingCollision>(); HealthScript obstShot = hit.collider.GetComponent <HealthScript>(); if (shot != null) { shot.Damage(2); var shotTransform = Instantiate(mgShotPrefab) as Transform; shotTransform.position = hit.point; } else if (obstShot != null) { obstShot.Damage(2); var shotTransform = Instantiate(mgShotPrefab) as Transform; shotTransform.position = hit.point; } } }
void Update() { if (!isBuildingEnabled) { if (ghost != null) { Destroy(ghost); ghostOn = false; } return; } Ray ray; RaycastHit[] hit; ray = Camera.main.ScreenPointToRay(Input.mousePosition); hit = Physics.RaycastAll(ray, Mathf.Infinity); for (int i = 0; i < hit.Length; i++) { if (hit[i].collider.tag == TerrainCollisionTag) { if (SelectedBuilding != LastSelectedBuilding && ghost != null) { Destroy(ghost); ghostOn = false; LastSelectedBuilding = SelectedBuilding; break; } if (!ghostOn) { ghost = (GameObject)Instantiate(Building[SelectedBuilding], new Vector3(hit[i].point.x, hit[i].point.y, hit[i].point.z), Quaternion.identity); ghost.name = ghost.name.Replace("(Clone)", "(Ghost)"); ghost.layer = 2; //ignore raycast layer ghostCollision = ghost.AddComponent <BuildingCollision>(); ghostOn = true; } if (ghost != null) { if (Input.GetMouseButtonDown(0) && !ghostCollision.Collided() && isFlat) { BuildingList bl = new BuildingList(); DestroyImmediate(ghost); bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding], new Vector3(hit[i].point.x, hit[i].point.y + Building[SelectedBuilding].GetComponent <Collider>().transform.localScale.y / 2, hit[i].point.z), Quaternion.identity); string s = bl.buildingGameObject.name.Replace("(Clone)", ""); bl.buildingGameObject.name = s; bl.buildingName = s; bl.buildingGameObject.AddComponent <BuildingCollision>(); buildings.Add(bl); ghostOn = false; DeactivateBuildingmode(); break; } Vector3 ghostTargetPos = new Vector3( hit[i].point.x, hit[i].point.y + Building[SelectedBuilding].GetComponent <Collider>().transform.localScale.y / 2, hit[i].point.z); ghost.transform.position = ghostTargetPos; isFlat = GroundFlat(); if (ghostCollision.Collided() || !isFlat) { ghost.GetComponent <Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent <Renderer>().sharedMaterial); ghost.GetComponent <Renderer>().material.color = new Color( 1f, 0f, 0f, 0.6f); } else if (!ghostCollision.Collided() && isFlat) { ghost.GetComponent <Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent <Renderer>().sharedMaterial); ghost.GetComponent <Renderer>().material.color = new Color( 0f, 1f, 0f, 0.6f); } } } } }
void Update() { if (!isBuildingEnabled) { if (ghost != null) { Destroy(ghost); ghostOn = false; } return; } /* * if (SelectedBuilding == 2) * { * TerrainCollisionTag = "MetalSpots"; * } * else * { * TerrainCollisionTag = "Floor"; * } */ Ray ray; RaycastHit[] hit; ray = Camera.main.ScreenPointToRay(Input.mousePosition); hit = Physics.RaycastAll(ray, Mathf.Infinity); if (Input.GetKey(KeyCode.Escape)) { DeactivateBuildingmode(); } for (int i = 0; i < hit.Length; i++) { if (hit[i].collider.tag == TerrainCollisionTag) { if (SelectedBuilding != LastSelectedBuilding && ghost != null) { Destroy(ghost); ghostOn = false; LastSelectedBuilding = SelectedBuilding; break; } if (!ghostOn) { ghost = (GameObject)Instantiate(Building[SelectedBuilding], new Vector3(hit[i].point.x, hit[i].point.y, hit[i].point.z), Quaternion.identity); ghost.name = ghost.name.Replace("(Clone)", "(Ghost)"); ghost.layer = 2; //ignore raycast layer if (!ghost.GetComponent <CastlePlayer>().BuildGuide.activeSelf) { ghost.GetComponent <CastlePlayer>().BuildGuide.SetActive(true); } ghostCollision = ghost.AddComponent <BuildingCollision>(); ghostOn = true; } if (ghost != null) { if (Input.GetMouseButtonDown(0) && !ghostCollision.Collided() && isFlat && ghost.GetComponent <CastlePlayer>().overlap == false) { //Debug.Log(ghost.GetComponent<UnitSpawner>().allowSpawn); BuildingList bl = new BuildingList(); DestroyImmediate(ghost); bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding], new Vector3(hit[i].point.x, hit[i].point.y + Building[SelectedBuilding].GetComponent <Collider>().transform.localScale.y / 2, hit[i].point.z), Quaternion.identity); string s = bl.buildingGameObject.name.Replace("(Clone)", ""); bl.buildingGameObject.name = s; bl.buildingName = s; bl.buildingGameObject.AddComponent <BuildingCollision>(); buildings.Add(bl); //ghost.GetComponent<UnitSpawner>().allowSpawn = true; ghostOn = false; DeactivateBuildingmode(); if (ghost.GetComponent <CastlePlayer>().BuildGuide.activeSelf) { ghost.GetComponent <CastlePlayer>().BuildGuide.SetActive(false); } break; } Vector3 ghostTargetPos = new Vector3( hit[i].point.x, hit[i].point.y + Building[SelectedBuilding].GetComponent <Collider>().transform.localScale.y / 2, hit[i].point.z); ghost.transform.position = ghostTargetPos; isFlat = GroundFlat(); /* * if (ghostCollision.Collided() || !isFlat) * { * ghost.GetComponent<Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent<Renderer>().sharedMaterial); * ghost.GetComponent<Renderer>().material.color = new Color( * 1f, * 0f, * 0f, * 0.6f); * } * else if (!ghostCollision.Collided() && isFlat) * { * ghost.GetComponent<Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent<Renderer>().sharedMaterial); * ghost.GetComponent<Renderer>().material.color = new Color( * 0f, * 1f, * 0f, * 0.6f); * } */ } } } }