public void Update() { //if not actively attacking etc, move randomly if (!moving && !agressiveMoving && !attacking && !movingBuilding) { walkToRandom(); } //if damage taken it triggeres a effect if (damageTaken) { if (Time.time <= damageTakenStart + damageTakenDuration) { if (Time.time <= damageTakenStart + damageTakenDuration / 2) { flashAmount = ((Time.time - damageTakenStart) / (damageTakenDuration / 2)); } else { flashAmount = 1 - ((Time.time - (damageTakenStart + damageTakenDuration / 2)) / (damageTakenDuration / 2)); } sr.material.SetFloat("Flash", flashAmount); } else { damageTaken = false; sr.material.SetFloat("Flash", 0); } } //after the scanningTime has passed it scans for units/buildings to attack if (Time.time - lastScan >= scanDelay) { bool found = false; if (!agressiveMoving && !attacking && !movingBuilding) { GameObject go = scanForUnitsOn(); if (go != null) { found = true; moving = false; attack(go); } else { Grid[] g = scanForBuildings(); if (g != null && g[0] != null && g[1] != null) { found = true; moving = false; targetBuilding = g[0]; targetBuildingMoveGrid = g[1]; Vector2 pos = getBuildingMovePos(); attackBuilding(pos); } } } if (!found) { lastScan = Time.time; } } //triggers the attack animation and deals damage to the target if (attacking || attackingBuilding) { if (animator.GetBool(ATTACKINGS) == false) { animator.SetBool(ATTACKINGS, true); } else { if (animationEventHandler.isAttackEnded()) { if (attacking) { damageTarget(damage); attacking = false; animator.SetBool(ATTACKINGS, false); } else if (attackingBuilding) { damageTargetBuilding(damage); attackingBuilding = false; animator.SetBool(ATTACKINGS, false); } } } return; } //movements (walking, attacking unit, attacking building) if (moving || agressiveMoving || movingBuilding) { if (moving) { type = WALKINGTYPE; types = WALKINGTYPES; } else if (agressiveMoving) { type = AGRESSIVEWALKINGTYPE; types = AGRESSIVEWALKINGTYPES; } else if (movingBuilding) { type = ATTACKINBUILDINGTYPE; types = AGRESSIVEWALKINGTYPES; } //if attacking unit if (agressiveMoving) { if (targetUnitUnit != null) { //check if the unit chaged grid/is moving, if it is recalculate the path if (targetUnitUnit.getGridOn() != targetGrid) { attack(targetUnit); } //check if close enogh to attack if (targetUnit != null && CalcUtil.getAbsDist(gameObject, targetUnit) <= range) { attacking = true; } } else { //if target is destroyed, clear agressive behaviour clearAgressiveMoving(); return; } } //if attacking building else if (movingBuilding) { if (targetBuilding != null && targetBuilding.isBuilding() && targetBuilding.getBlockingObject() != null) { //check if close enogh to attack if (CalcUtil.getAbsDist(transform.position.x, transform.position.y, targetBuildingMoveGrid.getPos().x, targetBuildingMoveGrid.getPos().y) <= buildingAttackRange) { moving = false; attackingBuilding = true; } } else { //if building is destroyed, clear agressive behaviour clearAttackingBuilding(); } } //if distance to the next move position is greater 0.1 move to it, else swap to the next one if (CalcUtil.getAbsDist(transform.position.x, transform.position.y, moveToPos.x, moveToPos.y) >= 0.1) { //if movepos is on the right, move right if (transform.position.x - moveToPos.x <= -0.01) { if (type == AGRESSIVEWALKINGTYPE || type == ATTACKINBUILDINGTYPE) { transform.position += new Vector3(attackingSpeed, 0); } else if (type == WALKINGTYPE) { transform.position += new Vector3(walkingSpeed, 0); } } //left else if (transform.position.x - moveToPos.x >= 0.01) { if (type == AGRESSIVEWALKINGTYPE || type == ATTACKINBUILDINGTYPE) { transform.position += new Vector3(-attackingSpeed, 0); } else if (type == WALKINGTYPE) { transform.position += new Vector3(-walkingSpeed, 0); } } //top if (transform.position.y - moveToPos.y <= -0.01) { if (type == AGRESSIVEWALKINGTYPE || type == ATTACKINBUILDINGTYPE) { transform.position += new Vector3(0, attackingSpeed); } else if (type == WALKINGTYPE) { transform.position += new Vector3(0, walkingSpeed); } } //bot else if (transform.position.y - moveToPos.y >= 0.01) { if (type == AGRESSIVEWALKINGTYPE || type == ATTACKINBUILDINGTYPE) { transform.position += new Vector3(0, -attackingSpeed); } else if (type == WALKINGTYPE) { transform.position += new Vector3(0, -walkingSpeed); } } } else { //if path index is 0 / path has ended, stop moving if (i <= 0) { moving = false; agressiveMoving = false; movingBuilding = false; animator.SetBool(types, false); setGridOn(movingPath.getPath()[i]); } //else, get next pose in path to move to else { setGridOn(movingPath.getPath()[i]); i--; if (i != 0) { moveToPos = movingPath.getPath()[i].getPos(); } else { moveToPos = movingPath.getDest(); } if (movingPath.getPath()[i].isPathBlocked()) { if (type == AGRESSIVEWALKINGTYPE && targetUnit != null) { attack(targetUnit); } else if (type == WALKINGTYPE) { walkToRandom(); } else if (type == ATTACKINBUILDINGTYPE) { attackBuilding(getBuildingMovePos()); } } //look to destination if (transform.position.x <= moveToPos.x) { lookingRight = true; transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.z); } else { lookingRight = false; transform.rotation = Quaternion.Euler(transform.rotation.x, 180, transform.rotation.z); } } } } }
public void Update() { //if damage taken, it does a visual effect if (damageTaken) { if (Time.time <= damageTakenStart + damageTakenDuration) { if (Time.time <= damageTakenStart + damageTakenDuration / 2) { flashAmount = ((Time.time - damageTakenStart) / (damageTakenDuration / 2)); } else { flashAmount = 1 - ((Time.time - (damageTakenStart + damageTakenDuration / 2)) / (damageTakenDuration / 2)); } sr.material.SetFloat("Flash", flashAmount); } else { damageTaken = false; sr.material.SetFloat("Flash", 0); } } //if attacking it does a animation and deals damage to the target if (attacking) { if (animator.GetBool(ATTACKINGS) == false) { animator.SetBool(ATTACKINGS, true); attackstart = Time.time; } else { if (animationEventHandler.isAttackEnded()) { damageTarget(damage); attacking = false; animator.SetBool(ATTACKINGS, false); } } return; } //if moving it moves to the move-to-location if (moving) { //if targets grid changed, refresh path if (targetUnitUnit != null && targetUnit.activeInHierarchy && targetUnitUnit.getGridOn() != targetGrid) { attack(targetUnit); return; } //if in range with the target, attack it if (targetUnit != null && targetUnit.activeInHierarchy && CalcUtil.getAbsDist(gameObject, targetUnit) <= range) { attacking = true; } //moving if (Mathf.Sqrt(Mathf.Pow(transform.position.x - moveToPos.x, 2)) >= 0.01 || Mathf.Sqrt(Mathf.Pow(transform.position.y - moveToPos.y, 2)) >= 0.01) { if (transform.position.x - moveToPos.x <= -0.01) { transform.position += new Vector3(speed, 0); } else if (transform.position.x - moveToPos.x >= 0.01) { transform.position += new Vector3(-speed, 0); } if (transform.position.y - moveToPos.y <= -0.01) { transform.position += new Vector3(0, speed); } else if (transform.position.y - moveToPos.y >= 0.01) { transform.position += new Vector3(0, -speed); } } //if moved to destination get next postiontion in path and move to it, until path is gone else { if (i <= 0) { moving = false; animator.SetBool(WALKINGTYPES, false); setGridOn(movingPath.getPath()[i]); } else { setGridOn(movingPath.getPath()[i]); i--; if (i != 0) { moveToPos = movingPath.getPath()[i].getPos(); } else { moveToPos = movingPath.getDest(); } if (movingPath.getPath()[i].isPathBlocked()) { moveTo(movingPath.getPath()[i]); } //looking in destinations direction if (transform.position.x <= moveToPos.x) { lookingRight = true; transform.rotation = Quaternion.Euler(transform.rotation.x, 0, transform.rotation.z); } else { lookingRight = false; transform.rotation = Quaternion.Euler(transform.rotation.x, 180, transform.rotation.z); } } } } }