/// <summary> /// Called when the given hit box comes into contact with one of this character's hurt boxes. /// This method inflicts damage to the character based on the hit box's stats /// </summary> public void OnCollision(Collider2D hitBox) { // Perform the 'Hit' action, making the character display his hit animation character.CharacterControl.PerformAction(actionSet.basicActions.hit); // Informs the character that was hit that his CharacterAI component must adapt to the fact that he was hit character.CharacterAI.OnHit(); // Cache the hit box's components to determine the properties of the hit box which hit this character HitBoxObject hitBoxObject = hitBox.GetComponent <HitBoxObject>(); // Inform the hit box that it just hit this character. The hit box will deal damage to the character and play impact sounds hitBoxObject.OnHit(character); }
/// <summary> /// Creates the hit box for the given action. /// </summary> private void CreateHitBoxes(Action action) { // Cycle through each HitBox assigned to the action for (int i = 0; i < action.hitBoxes.Length; i++) { // Cache the HitBox being cycled through HitBox hitBoxInfo = action.hitBoxes[i]; // Create a new GameObject for the hit box GameObject gameObject = new GameObject("HitBox"); gameObject.transform.parent = transform; // TODO: Cache both Transforms gameObject.layer = Brawler.Layer.HitBox; // This hit box inflicts damage // Attach a collider to the hit box BoxCollider2D collider = gameObject.AddComponent <BoxCollider2D>(); collider.offset = hitBoxInfo.offset; collider.size = hitBoxInfo.size; collider.isTrigger = true; // Creates a BoneFollower instance which allows the HitBox to follow a bone's actionment BoneFollower boneFollower = gameObject.AddComponent <BoneFollower>(); boneFollower.SkeletonRenderer = skeleton; boneFollower.boneName = hitBoxInfo.boneName; boneFollower.followBoneRotation = true; boneFollower.followZPosition = true; // Creates a hitBoxObject component so that the hit box can keep a reference to the HitBox object that it represents HitBoxObject hitBox = gameObject.AddComponent <HitBoxObject>(); hitBox.HitBoxInfo = hitBoxInfo; // Cache the HitBox's GameObject and components inside the data container object hitBoxInfo.GameObject = gameObject; hitBoxInfo.Character = character; hitBoxInfo.Action = action; hitBoxInfo.Collider = collider; // Disable the hit box until the attack action is performed. hitBoxInfo.Disable(); } }