// Update is called once per frame void Update() { //Select a unit if (Input.GetMouseButtonDown(0)) { //Get mouse position RaycastHit hit = JUCLPhysicsUtils.GetMousePositionRaycastData(Camera.main, selectionLayers); if (hit.collider != null) { //Set selected transform to transform from collider selectTransform = hit.collider.transform; } } //Move a unit if (selectTransform != null && Input.GetMouseButtonDown(1)) { //Get mouse position RaycastHit hit = JUCLPhysicsUtils.GetMousePositionRaycastData(Camera.main, targetLayers); //Get selected unit AIUnit unit = selectTransform.GetComponent <AIUnit>(); if (unit != null) { //Move unit unit.MoveTo(hit.point, 0.1f, null); } else { //Log error Debug.LogError("Unit is null"); } } //Follow toggling if (Input.GetKeyDown(KeyCode.F)) { //Toggle follow mode if (cameraController.followTransform == null) { cameraController.followTransform = selectTransform; } else { cameraController.followTransform = null; } } //Target following if (Input.GetKeyDown(KeyCode.Escape)) { //Stop following target cameraController.followTransform = null; selectTransform = null; } }
// Update is called once per frame void Update() { //Test for selecting buildings if (Input.GetKeyDown(KeyCode.Alpha0)) { selectedBuilding = 0; } if (Input.GetKeyDown(KeyCode.Alpha1)) { selectedBuilding = 1; } //if (Input.GetKeyDown(KeyCode.Alpha2)) //{ // selectedBuilding = 2; //} //if (Input.GetKeyDown(KeyCode.Alpha3)) //{ // selectedBuilding = 3; //} //Range check, checks selected building is valid ID if (selectedBuilding >= 1 && selectedBuilding < buildings.Length + 1) { //Makes sure this code is only executed upon building selection change if (lastSelectedBuilding != selectedBuilding) { //Destroy previous building ghost if (ghostBuilding != null) { Destroy(ghostBuilding); } //Get mouse position and spawn new ghost building RaycastHit hit = JUCLPhysicsUtils.GetMousePositionRaycastData(Camera.main, layersToCheck); ghostBuilding = Instantiate(buildings[selectedBuilding - 1].buildingPrefab, hit.point, Quaternion.identity); lastSelectedBuilding = selectedBuilding; } } //If building is selected if (ghostBuilding != null) { //Get mouse position and move object to mouse cursor RaycastHit hit = JUCLPhysicsUtils.GetMousePositionRaycastData(Camera.main, layersToCheck); //Snap to grid if (Input.GetKey(KeyCode.LeftControl)) { //Snap position ghostBuilding.transform.position = new Vector3( Mathf.Round(hit.point.x / snapGridSize) + snapGridSize / 2, Mathf.Round(hit.point.y / snapGridSize) + snapGridSize / 2, Mathf.Round(hit.point.z / snapGridSize) + snapGridSize / 2); } else { //Don't snap to grid ghostBuilding.transform.position = hit.point; } //Rotating the building to be placed. if (Input.GetKeyDown(KeyCode.E)) { ghostBuilding.transform.Rotate(0, 45, 0); } if (Input.GetKeyDown(KeyCode.Q)) { ghostBuilding.transform.Rotate(0, -45, 0); } //Resource cost check if (buildings[selectedBuilding - 1].goldCost <= GameResources.GetGoldAmount()) { ChangeBuildingMaterial(ghostValidMat); //If user clicks to place object if (Input.GetMouseButtonDown(0)) { //Create new object and remove gold from player resources GameObject go = Instantiate(buildings[selectedBuilding - 1].buildingPrefab, ghostBuilding.transform.position, ghostBuilding.transform.rotation); Building building = go.GetComponent <Building>(); building.CreateScaffolding(); building.AddBuilder(); GameResources.RemoveGoldAmount(buildings[selectedBuilding - 1].goldCost); //Destroy ghost building if shift is not held down, also reset selection if (!Input.GetKey(KeyCode.LeftShift)) { Destroy(ghostBuilding); selectedBuilding = 0; lastSelectedBuilding = 0; } } } else { ChangeBuildingMaterial(ghostInvalidMat); } //Destroy ghostbuilding if escape pressed, reset selection. if (Input.GetKeyDown(KeyCode.Escape)) { Destroy(ghostBuilding); selectedBuilding = 0; lastSelectedBuilding = 0; } } }