Unity PlatForm 09 Enemy Damaged === 2020.08.14 ###### tags: `platform` ## List 1. [Enermy Damaged](#ED) 2. [Knockback & Die](#KD) 3. [Particle System](#PS) ## Content ### Enemry Damaged<a id=ED/> When player attack enermies, show animation, decrease hp, finally dead.<br>Set tags for enemry animations, and control in script.<br>Need some conditions and objects below. ```csharp= // This for MoveSpeed with anim. [SerializeField] private float maxHealth, knockbackSpeedX, knockbackSpeedY, knockbackDuration, knockbackDeathSpeedX, knockbackDeathSpeedY, deathToque; [SerializeField] private bool applyKnockback; [SerializeField] private GameObject hitParticle; private float currentHealth, knockbackStart; private int playerFacingDirection; private bool playerOnLeft, knockback; //Player Class is for damage section and facing... private PlayerPlatformAdvenced pc; private GameObject aliveGO, brokenTopGO, brokenBotGO; private Rigidbody2D rbAlive, rbBrokenTop, rbBrokenBot; private Animator aliveAnim; ``` <br> Damage function is activated in playerCombat script, `collider.transform.parent.SendMessage("Damage", attackDamage);`<br>Set all action and trigger.<br>Set animation direction with player's relative position.<br>So, `GetFacingDirection()` is written in Player Script. ```csharp= private void Damage(float amout) { currentHealth -= amout; playerFacingDirection = pc.GetFacingDirection(); Instantiate(hitParticle, aliveGO.transform.position, Quaternion.Euler(0.0f, 0.0f, Random.Range(0.0f, 360.0f))); if (playerFacingDirection == 1) playerOnLeft = true; else playerOnLeft = false; aliveAnim.SetBool("playerOnLeft", playerOnLeft); aliveAnim.SetTrigger("damage"); if (applyKnockback && currentHealth > 0.0f) Knockback(); if (currentHealth <= 0) Die(); } ``` <br><br> ### Knockback & Die<a id=KD/> Enemies are damaged, knockbacking or dying.<br>Knckback technic is same player's action.<br>Check Condition with time in `update()`. ```csharp= private void Knockback() { knockback = true; knockbackStart = Time.time; rbAlive.velocity = new Vector2(knockbackSpeedX * playerFacingDirection, knockbackSpeedY); } // In update() private void Checkknockback() { if (Time.time >= knockbackStart + knockbackDuration && knockback) { knockback = false; rbAlive.velocity = new Vector2(0, rbAlive.velocity.y); } } ``` <br> Last, HP reach 0, enemies die.<br>When die, broken objs actived, punishing other side and rotating.<br>Use torque for this movement. ```csharp= private void Die() { aliveGO.SetActive(false); brokenTopGO.SetActive(true); brokenBotGO.SetActive(true); brokenTopGO.transform.position = aliveGO.transform.position; brokenBotGO.transform.position = aliveGO.transform.position; rbBrokenBot.velocity = new Vector2(knockbackSpeedX * playerFacingDirection, knockbackSpeedY); rbBrokenTop.velocity = new Vector2(knockbackDeathSpeedX * playerFacingDirection, knockbackDeathSpeedY); rbBrokenTop.AddTorque(deathToque * -playerFacingDirection, ForceMode2D.Impulse); } ``` <br> ### Particle System<a id="PS"/> When attack, show particles between player and enermies.<br>Attack particles are instatiated in `Damage()`, must be destroyed end attack animation.<br>It is not limited in attack, possible any other particle effects. ```csharp= public class ParticleController : MonoBehaviour { private void FinishAnim() { Destroy(gameObject); } } ```