Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CombatDummyController : MonoBehaviour
- {
- [SerializeField]
- private float maxHealth, knockbackspeedX, knockbackspeedY, knockbackDuration, knockbackDeathspeedX, knockbackDeathspeedY, deathTorque;
- [SerializeField]
- private bool applyKnockback;
- private float currentHealth, knockbackStart;
- private int playerFacingDirection;
- private bool playerOnLeft, knockback;
- private PlayerController pc;
- private GameObject aliveGO, brokenTopGO, brokenBotGO;
- private Rigidbody2D rbAlive, rbBrokenTop, rbBrokenBot;
- private Animator aliveAnim;
- public GameObject hitParticle;
- private void Start()
- {
- currentHealth = maxHealth;
- pc = GameObject.Find("Player").GetComponent<PlayerController>();
- aliveGO = transform.Find("Alive").gameObject;
- brokenTopGO = transform.Find("Broken Top").gameObject;
- brokenBotGO = transform.Find("Broken Bottom").gameObject;
- aliveAnim = aliveGO.GetComponent<Animator>();
- rbAlive = aliveGO.GetComponent<Rigidbody2D>();
- rbBrokenTop = brokenTopGO.GetComponent<Rigidbody2D>();
- rbBrokenBot = brokenBotGO.GetComponent<Rigidbody2D>();
- aliveGO.SetActive(true);
- brokenTopGO.SetActive(false);
- brokenBotGO.SetActive(false);
- }
- public void Damage(float amount)
- {
- currentHealth -= amount;
- 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();
- Knockback();
- }
- if (currentHealth <= 0.0f)
- {
- // Die();
- Die();
- }
- }
- private void Knockback()
- {
- knockback = true;
- knockbackStart = Time.time;
- rbAlive.velocity = new Vector2(knockbackspeedX * playerFacingDirection, knockbackspeedY);
- }
- private void CheckKnockback()
- {
- if(Time.time >= knockbackStart + knockbackDuration && knockback)
- {
- knockback = false;
- rbAlive.velocity = new Vector2(0.0f, rbAlive.velocity.y);
- }
- }
- 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(deathTorque * -playerFacingDirection, ForceMode2D.Impulse);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement