private void Attack(MazeUnit possibleTarget) { Health targetHealth = possibleTarget.GetComponent <Health>(); if (!targetHealth) { Debug.LogError(gameObject.name + " is trying to attack a target with no Health: " + possibleTarget.name); return; } targetHealth.TakeDamage(attackDamage); }
private void Attack() { MazeUnit target = Maze.Instance.GetOccupant(attackPos); if (target) { Health targetHealth = target.GetComponent <Health>(); if (targetHealth) { targetHealth.TakeDamage(damage); Used(); } } }
public static void ReplaceUnit(beMobileMaze hostMaze, MazeUnit obtainedUnit, MazeUnit unitContainingReplacements, bool resetScaling = true, bool forceRecalculationOfComponentPosition = true) { #region prepare children lists int new_ChildCount = unitContainingReplacements.transform.childCount; List <GameObject> new_Children = new List <GameObject>(); int old_ChildCount = obtainedUnit.transform.childCount; List <GameObject> existing_children = new List <GameObject>(); for (int i = 0; i < old_ChildCount; i++) { var old_child = obtainedUnit.transform.GetChild(i); existing_children.Add(old_child.gameObject); } for (int i = 0; i < new_ChildCount; i++) { var new_child = unitContainingReplacements.transform.GetChild(i); new_Children.Add(new_child.gameObject); } #endregion if (resetScaling) { obtainedUnit.transform.localScale = Vector3.one; } var dimension = unitContainingReplacements.Dimension; var boxCollider = obtainedUnit.GetComponent <BoxCollider>(); boxCollider.size = dimension; boxCollider.center = new Vector3(0, boxCollider.size.y / 2, 0); foreach (var replacementPart in new_Children) { if (existing_children.Any((go) => go.name.Equals(replacementPart.name))) { var old_equivalent = existing_children.Single((go) => go.name.Equals(replacementPart.name)); if (resetScaling) { old_equivalent.transform.localScale = Vector3.one; } replacementPart.transform.SetParent(old_equivalent.transform.parent, false); old_equivalent.transform.parent = null; if (old_equivalent.name.Equals(MazeUnit.TOP)) { replacementPart.transform.localPosition = new Vector3(0, dimension.y, 0); } if (old_equivalent.name.Equals(MazeUnit.NORTH)) { replacementPart.transform.localPosition = new Vector3(0, dimension.y / 2, dimension.z / 2); } if (old_equivalent.name.Equals(MazeUnit.SOUTH)) { replacementPart.transform.localPosition = new Vector3(0, dimension.y / 2, -dimension.z / 2); } if (old_equivalent.name.Equals(MazeUnit.WEST)) { replacementPart.transform.localPosition = new Vector3(-dimension.x / 2, dimension.y / 2, 0); } if (old_equivalent.name.Equals(MazeUnit.EAST)) { replacementPart.transform.localPosition = new Vector3(dimension.x / 2, dimension.y / 2, 0); } if (old_equivalent.name.Equals(MazeUnit.FLOOR)) { replacementPart.transform.localPosition = new Vector3(0, 0, 0); } var meshRenderer = replacementPart.GetComponent <MeshRenderer>(); var oldMeshRenderer = old_equivalent.GetComponent <MeshRenderer>(); if (meshRenderer != null && oldMeshRenderer != null) { meshRenderer.sharedMaterials = oldMeshRenderer.sharedMaterials; } replacementPart.SetActive(old_equivalent.activeSelf); } } foreach (var item in existing_children) { UnityEngine.Object.DestroyImmediate(item.gameObject); } }