void Update() { float timeInterval = Time.time - startTime; gameObject.transform.position = Vector3.Lerp(startPosition, targetPosition, timeInterval * speed / distance); if (gameObject.transform.position.Equals(targetPosition)) { if (target != null) { //Transform healthBarTransform = target.transform.FindChild("HealthBar"); //HealthBar healthBar = healthBarTransform.gameObject.GetComponent<HealthBar>(); //healthBar.currentHealth -= Mathf.Max(damage, 0); //if (healthBar.currentHealth <= 0) { DestoryMonster(target); /*玩家分数增加 */ } } Destroy(gameObject); towerShoot = null; } }
// Update is called once per frame void Update() { towerTransform = GameObject.Find("Tower"); TowerShoot towerScript = towerTransform.GetComponent <TowerShoot>(); targetID = towerScript.targetID; GameObject[] troopList = GameObject.FindGameObjectsWithTag("Troop"); foreach (GameObject troop in troopList) { int troopID = troop.GetInstanceID(); if (troopID == targetID) { targetTroop = troop; } } Vector2 displacement = targetTroop.transform.position - transform.position; Vector2 directionToTroop = displacement.normalized; Vector2 projectileVelocity = directionToTroop * projectileSpeed; transform.Translate(projectileVelocity * Time.deltaTime); }
// Move the selected piece if one is selected void MoveSelection() { // Check if a place has been selected if (selectedTower != null) { // Create a new ray from the camera Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); GizmosGL.color = Color.yellow; GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * rayDistance, 0.1f, 0.1f); RaycastHit hit; Vector3 hitPoint = Vector3.zero; // Raycast to only hit objects that aren't towers if (Physics.Raycast(ray, out hit, rayDistance, ~selectionIgnoreLayer)) { // Obtain the hit point GizmosGL.color = Color.blue; GizmosGL.AddSphere(hit.point, 0.5f); hitPoint = hit.point; // Move the tower to position Vector3 piecePos = hit.point + Vector3.up * pieceHeight; selectedTower.transform.position = piecePos; } // Check if mouse button is released if (Input.GetMouseButtonUp(0)) { // Drop the piece on hitPoint selectedTower.transform.position = hitPoint; // Deselect the piece selectedTower = null; } } }
// Check if we are selecting a piece void CheckSelection() { // If there is already a selected piece if (selectedTower != null) { return; // Exit the function } // Creating a ray from camera mouse position to world Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); GizmosGL.color = Color.red; GizmosGL.AddLine(ray.origin, ray.origin + ray.direction * rayDistance); // Check if the player hits the mouse button if (Input.GetMouseButtonDown(0)) { RaycastHit hit; // Cast a ray to detect if (Physics.Raycast(ray, out hit, rayDistance)) { // Set the selected object to be the hit object selectedTower = hit.collider.GetComponent <TowerShoot>(); // If the user did not hit an object if (selectedTower == null) { Debug.Log("Cannot pick up object: " + hit.collider.name); } } } }
void Awake() { _towerTarget = GetComponent <TowerTarget> (); _towerShoot = GetComponent <TowerShoot> (); _renderer = GetComponent <SpriteRenderer> (); currentUpgrade = 0; maxAmountOfUpgrades = upgrades.Length; }
private IEnumerator Build(GameObject obj) { bool isBuilding = true; GameObject tower = Instantiate(obj, Vector3.zero, Quaternion.identity) as GameObject; tower.transform.SetParent(_towersObject.transform); TowerInputController towerInputController = tower.GetComponent <TowerInputController>(); TowerShoot towerShoot = tower.GetComponent <TowerShoot>(); TowerTarget towerTarget = tower.GetComponent <TowerTarget>(); BoxCollider2D collider = tower.GetComponent <BoxCollider2D>(); SpriteRenderer renderer = tower.GetComponent <SpriteRenderer>(); Color defaultColor = renderer.color; collider.isTrigger = true; towerInputController.enabled = false; towerShoot.enabled = false; towerTarget.enabled = false; while (isBuilding) { Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 roundedPosition = new Vector2(Mathf.RoundToInt(mouseWorldPosition.x), Mathf.RoundToInt(mouseWorldPosition.y)); tower.transform.position = roundedPosition; RaycastHit2D[] hitInfo = Physics2D.RaycastAll(mouseWorldPosition, Vector2.zero, Mathf.Infinity, _layerMask); bool isPlaceAbleTile = CheckForPlaceablePosition(hitInfo); if (isPlaceAbleTile) { renderer.color = Color.green; } else { renderer.color = Color.red; } if (Input.GetMouseButtonDown(0) && isPlaceAbleTile) { isBuilding = false; } yield return(new WaitForSeconds(0f)); } towerInputController.enabled = true; towerShoot.enabled = true; towerTarget.enabled = true; renderer.color = defaultColor; }
void Start() { LR = GetComponent <LineRenderer>(); LR.SetVertexCount(resolution); eachvertex = 360f / (resolution - 1); TSdata = GetComponent <TowerShoot>(); viewRadius = TSdata.TowerData.TowerData.Upgrade[TSdata.IndexLevel].Range; StartCoroutine(findTargetWithDelay(0.1f)); }
private void Start() { towerShoot = GetComponent <TowerShoot>(); Upgrade(); }
void Awake() { _towerShoot = GetComponent <TowerShoot> (); }