public void LocateBuildingOnMap(GameObject building) { Vector3 pos = GetRandomPosition(); if ((GetNearColliders(pos) == null || GetNearColliders(pos).Length == 0) && Move_out_of_zone.OnZone_Characters(pos.x, pos.y)) { Instantiate(building, pos, Quaternion.identity); } else { LocateBuildingOnMap(building); } }
void Update() { bool moving = GetComponent <Walk>().IsMoving(); if (attacking && !moving) { rb.velocity = new Vector3(0, 0, 0); } if (attackedAnimator != null)//the attacker died { if (attackedAnimator.GetBool("die")) { animator.SetBool("toAttack", false); } } if (this.tag.Contains("colony")) { bool detectedTouch = Input.GetMouseButtonDown(0); Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition); bool detectedTouchInZone = Move_out_of_zone.OnZone_Characters(p.x, p.y); if (detectedTouch) { //touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); //Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y); //RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Vector2.zero); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hitInformation = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity); Vector3 mousePos = Input.mousePosition; mousePos = Camera.main.ScreenToWorldPoint(mousePos); Collider2D[] nearSoldiers1 = GetNearSoldiers(mousePos); if ((hitInformation.collider != null && nearSoldiers1 != null && nearSoldiers1.Length > 0) && !soldierChosen) { Transform t = nearSoldiers1[0].transform; float dis = Mathf.Infinity; foreach (Collider2D collider in nearSoldiers1) { float currDis = Mathf.Abs(Vector3.Distance(collider.transform.position, mousePos)); if (currDis < dis) { dis = currDis; t = collider.transform; } } if (this.tag == t.tag) { soldierChosen = true; } } else if (soldierChosen) { Collider2D[] nearSoldiers2 = GetNearSoldiers(mousePos); if (nearSoldiers2 != null && nearSoldiers2.Length > 0) { targetToAttack = (nearSoldiers2[0].name.Split(' '))[0]; targetChosen = true; } else { targetChosen = false; } soldierChosen = false; } } } if (this.tag.Contains("colony")) { bool solAttacked = attacked != null; bool solAttackedAnimator = attacked != null; if (solAttacked && solAttackedAnimator) { setAttack = true; Attack attackCompOfAttacked = attacked.GetComponent <Attack>(); if (attackCompOfAttacked != null) { GameObject attackedOfAttacked = attackCompOfAttacked.GetAttacked(); //if (attackCompOfAttacked != null) //{ // AttackCollision(attackedOfAttacked); //} } } else { setAttack = false; } } }