Advertisement
Guest User

Dummy Controller

a guest
Jan 28th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CombatDummyController : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     private float maxHealth, knockbackspeedX, knockbackspeedY, knockbackDuration, knockbackDeathspeedX, knockbackDeathspeedY, deathTorque;
  9.     [SerializeField]
  10.     private bool applyKnockback;
  11.  
  12.     private float currentHealth, knockbackStart;
  13.  
  14.     private int playerFacingDirection;
  15.  
  16.     private bool playerOnLeft, knockback;
  17.  
  18.     private PlayerController pc;
  19.     private GameObject aliveGO, brokenTopGO, brokenBotGO;
  20.     private Rigidbody2D rbAlive, rbBrokenTop, rbBrokenBot;
  21.     private Animator aliveAnim;
  22.  
  23.     public GameObject hitParticle;
  24.  
  25.     private void Start()
  26.     {
  27.         currentHealth = maxHealth;
  28.  
  29.         pc = GameObject.Find("Player").GetComponent<PlayerController>();
  30.  
  31.         aliveGO = transform.Find("Alive").gameObject;
  32.         brokenTopGO = transform.Find("Broken Top").gameObject;
  33.         brokenBotGO = transform.Find("Broken Bottom").gameObject;
  34.  
  35.         aliveAnim = aliveGO.GetComponent<Animator>();
  36.         rbAlive = aliveGO.GetComponent<Rigidbody2D>();
  37.         rbBrokenTop = brokenTopGO.GetComponent<Rigidbody2D>();
  38.         rbBrokenBot = brokenBotGO.GetComponent<Rigidbody2D>();
  39.  
  40.         aliveGO.SetActive(true);
  41.         brokenTopGO.SetActive(false);
  42.         brokenBotGO.SetActive(false);
  43.     }
  44.    
  45.     public void Damage(float amount)
  46.     {
  47.         currentHealth -= amount;
  48.         playerFacingDirection = pc.GetFacingDirection();
  49.  
  50.         Instantiate(hitParticle, aliveGO.transform.position, Quaternion.Euler(0.0f, 0.0f, Random.Range(0.0f, 360.0f)));
  51.  
  52.         if (playerFacingDirection == 1)
  53.         {
  54.             playerOnLeft = true;
  55.         }
  56.         else
  57.         {
  58.             playerOnLeft = false;
  59.         }
  60.  
  61.         aliveAnim.SetBool("playerOnLeft", playerOnLeft);
  62.         aliveAnim.SetTrigger("damage");
  63.  
  64.         if (applyKnockback && currentHealth > 0.0f)
  65.         {
  66.             // Knockback();
  67.             Knockback();
  68.         }
  69.  
  70.         if (currentHealth <= 0.0f)
  71.         {
  72.             // Die();
  73.             Die();
  74.  
  75.         }
  76.     }
  77.  
  78.     private void Knockback()
  79.     {
  80.         knockback = true;
  81.         knockbackStart = Time.time;
  82.  
  83.         rbAlive.velocity = new Vector2(knockbackspeedX * playerFacingDirection, knockbackspeedY);
  84.     }
  85.  
  86.     private void CheckKnockback()
  87.     {
  88.         if(Time.time >= knockbackStart + knockbackDuration && knockback)
  89.         {
  90.             knockback = false;
  91.             rbAlive.velocity = new Vector2(0.0f, rbAlive.velocity.y);
  92.         }
  93.     }
  94.  
  95.     private void Die()
  96.     {
  97.         aliveGO.SetActive(false);
  98.         brokenTopGO.SetActive(true);
  99.         brokenBotGO.SetActive(true);
  100.  
  101.         brokenTopGO.transform.position = aliveGO.transform.position;
  102.         brokenBotGO.transform.position = aliveGO.transform.position;
  103.  
  104.         rbBrokenBot.velocity = new Vector2(knockbackspeedX * playerFacingDirection, knockbackspeedY);
  105.         rbBrokenTop.velocity = new Vector2(knockbackDeathspeedX * playerFacingDirection, knockbackDeathspeedY);
  106.         rbBrokenTop.AddTorque(deathTorque * -playerFacingDirection, ForceMode2D.Impulse);
  107.  
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement