GoodNoodle

Knockback.cs

Sep 23rd, 2021 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class KnockBack : MonoBehaviour
  6. {
  7.  
  8.     public Rigidbody2D theRB;
  9.     public float thrust;
  10.     public float knockTime;
  11.     public string otherTag;
  12.     private void OnTriggerEnter2D(Collider2D other)
  13.     {
  14.         if(other.gameObject.CompareTag("Enemy") && other.isTrigger)
  15.         {
  16.             Rigidbody2D hit = other.GetComponentInParent<Rigidbody2D>();
  17.             if (hit != null)
  18.             {
  19.                
  20.                 // hit.AddForce(difference, ForceMode2D.Impulse);
  21.  
  22.                 if (other.gameObject.CompareTag(otherTag) && other.isTrigger)
  23.                 {
  24.                     hit.GetComponent<EnemyController>().currentState = EnemyStates.stagger;
  25.                     other.GetComponent<EnemyController>().Knock(hit, knockTime);
  26.                 }
  27.                
  28.                 if (other.GetComponent<PlayerController>().state != PlayerState.stagger)
  29.                 {
  30.                     hit.GetComponent<PlayerController>().state = PlayerState.stagger;
  31.                     Knock(knockTime);
  32.                 }
  33.            
  34.             }
  35.         }
  36.     }
  37.  
  38.     public void Knock(float knocktime)
  39.     {
  40.         StartCoroutine(Knockco(knocktime));
  41.     }
  42.  
  43.     private IEnumerator Knockco(float knockTime)
  44.     {
  45.         if (theRB != null)
  46.         {
  47.             yield return new WaitForSeconds(knockTime);
  48.             theRB.velocity = Vector2.zero;
  49.             theRB.velocity = Vector2.zero;
  50.         }
  51.     }
  52. }
  53.  
  54.  
Add Comment
Please, Sign In to add comment