void OnTeamChanged() { _selectedUnit = null; softwareMinimap.team = team; teamColorGraphic.color = teamColors[team]; _platform.SetCameraTeam(mainCamera, team); // make it so only the current team will update their texture (this is an optimisation) foreach (FogOfWarTeam fowteam in FogOfWarTeam.instances) { fowteam.outputToTexture = fowteam.team == team; } }
void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex); return; } // select unit if (Input.GetKeyDown(KeyCode.Mouse0)) { RaycastHit hit; if (Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out hit))//, unitLayer)) { FogOfWarUnit unit = hit.collider.GetComponent <FogOfWarUnit>(); if (unit != null && unit.team == _fog.team) { int index = _units.FindIndex(((u) => u.unit == unit)); if (index != -1) { _units.Add(_units[index]); _units.RemoveAt(index); } else { _units.Add(new FOWUnit(unit)); } } } } // move unit if (_units.Count > 0 && (Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.Mouse2))) { RaycastHit[] hits = Physics.RaycastAll(_camera.ScreenPointToRay(Input.mousePosition)); if (hits.Length > 0) { Vector3 p = hits[hits.Length - 1].point; p.y = 1.0f; _units[_units.Count - 1].destination = p; } } // update units float moveamount = unitMoveSpeed * Time.deltaTime; for (int i = 0; i < _units.Count; ++i) { FOWUnit u = _units[i]; Vector3 direction = u.destination - u.position; direction.y = 0.0f; if (direction.sqrMagnitude < moveamount * moveamount) { u.position = new Vector3(u.destination.x, u.position.y, u.destination.z); } else { u.position += direction.normalized * moveamount; u.transform.rotation = Quaternion.Slerp(u.transform.rotation, Quaternion.LookRotation(direction, Vector3.up), moveamount); } } // update highlight if (_units.Count > 0) { highlight.position = new Vector3(_units[_units.Count - 1].position.x, 0.1f, _units[_units.Count - 1].position.z); highlight.gameObject.SetActive(true); } // update camera _cameraTransform.position += new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * (Time.deltaTime * cameraSpeed); if (Input.touchCount == 1) { Vector2 delta = Input.GetTouch(0).deltaPosition; _cameraTransform.position += new Vector3(-delta.x, 0, -delta.y); } // update camera zooming float zoomchange = Input.GetAxis("Mouse ScrollWheel"); _cameraTransform.position = new Vector3(_cameraTransform.position.x, Mathf.Clamp(_cameraTransform.position.y - zoomchange * 10, 25, 50), _cameraTransform.position.z); }
void Update() { // change team if (Input.GetKeyDown(KeyCode.Tab)) { team = (team + 1) % FogOfWarTeam.instances.Count; OnTeamChanged(); } // reset fog if (Input.GetKeyDown(KeyCode.R)) { _team.Reinitialize(); } // toggle clear fog if (Input.GetKeyDown(KeyCode.C)) { clearFog.enabled = !clearFog.enabled; } // toggle minimap if (Input.GetKeyDown(KeyCode.M)) { softwareMinimap.gameObject.SetActive(!softwareMinimap.gameObject.activeSelf); hardwareMinimap.SetActive(!hardwareMinimap.activeSelf); } // select unit if (Input.GetKeyDown(KeyCode.Mouse0) && Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) { FogOfWarUnit unit = hit.collider.GetComponent <FogOfWarUnit>(); _selectedUnit = null; if (unit != null && unit.team == team) { int index = _units.FindIndex(((u) => u.unit == unit)); if (index != -1) { _selectedUnit = _units[index]; } } } // move selected unit if (_selectedUnit != null && Input.GetKeyDown(KeyCode.Mouse1)) { if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit selecthit)) { Vector3 p = selecthit.point; p.y = _selectedUnit.position.y; _selectedUnit.destination = p; } } // clear fogged area if (Input.GetKeyDown(KeyCode.Mouse2) && Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit)) { _team.SetFog(new Bounds(hit.point, Vector3.one * unfogSize), 0); } // update unit movements float moveamount = unitMoveSpeed * Time.deltaTime; foreach (FOWUnit unit in _units) { // find new position to move to if (unit.destination == null) { if (unit.team != team) { float maxmovement = 10; unit.destination = unit.position + new Vector3(Random.Range(-maxmovement, maxmovement), 0, Random.Range(-maxmovement, maxmovement)); if (!Physics.Raycast(unit.destination.Value + Vector3.up, Vector3.down)) { unit.destination = null; } } if (unit.destination == null) { continue; } } // perform movement Vector3 offset = unit.destination.Value - unit.position; unit.position = Vector3.MoveTowards(unit.position, unit.destination.Value, moveamount); if (offset.sqrMagnitude > 0.01f) { unit.transform.rotation = Quaternion.Slerp(unit.transform.rotation, Quaternion.LookRotation(offset, Vector3.up), moveamount); } // if we have arrived at the destination if (offset.sqrMagnitude < 0.01f) { unit.destination = null; } } // update highlight if (_selectedUnit != null) { Vector3 pos = _selectedUnit.transform.position; pos.y = 0.1f; highlight.position = pos; highlight.gameObject.SetActive(true); } else { highlight.gameObject.SetActive(false); } // update camera Transform camtransform = mainCamera.transform; camtransform.position += new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * (Time.deltaTime * cameraSpeed); if (Input.touchCount == 1) { Vector2 delta = Input.GetTouch(0).deltaPosition; camtransform.position += new Vector3(-delta.x, 0, -delta.y); } // update camera zooming float zoomchange = Input.GetAxis("Mouse ScrollWheel"); camtransform.position = new Vector3(camtransform.position.x, Mathf.Clamp(camtransform.position.y - zoomchange * 10, 25, 50), camtransform.position.z); }