SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.AI;
  7.  
  8. public class ForcePush : MonoBehaviour
  9. {
  10.     Vector3 direction;
  11.     [SerializeField] float force;
  12.     [SerializeField] float maxForce;
  13.     [SerializeField] float forceBuildUpAmount;
  14.     [SerializeField] float radius;
  15.     [SerializeField] float radiusBuildUpAmount;
  16.  
  17.     PlayerMana mana;
  18.     [SerializeField] float manaCost;
  19.     [SerializeField] float manaBuildUpAmount;
  20.     [SerializeField] float cooldown;
  21.     bool abilityReady;
  22.  
  23.     List<GameObject> enemies = new List<GameObject>();
  24.  
  25.     [SerializeField] ParticleSystem forcePushCast;
  26.  
  27.     Transform runtimeDump;
  28.  
  29.     //##########################################################################
  30.     private void Start()
  31.     {
  32.         runtimeDump = GameObject.Find("RuntimeDump").transform;
  33.  
  34.         abilityReady = true;
  35.  
  36.         mana = GetComponentInParent<PlayerMana>();
  37.  
  38.         foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy")) // add boss!?
  39.         {
  40.             enemies.Add(enemy);
  41.         }
  42.     }
  43.  
  44.     //##########################################################################
  45.     private void FixedUpdate()
  46.     {
  47.         if(abilityReady)
  48.         {
  49.             BuildUp();
  50.             Cast();
  51.         }
  52.     }
  53.  
  54.     //##########################################################################
  55.     private void BuildUp()
  56.     {
  57.         if(Input.GetButtonDown("Fire2"))
  58.         {
  59.             manaCost = 0f;
  60.         }
  61.  
  62.         if (Input.GetButton("Fire2"))
  63.         {
  64.             if (force < maxForce)
  65.             {
  66.                 force += forceBuildUpAmount * Time.deltaTime;
  67.                 radius += radiusBuildUpAmount * Time.deltaTime;
  68.                 manaCost += manaBuildUpAmount * Time.deltaTime;
  69.             }
  70.         }
  71.     }
  72.  
  73.     //##########################################################################
  74.     private void Cast()
  75.     {
  76.         if (Input.GetButtonUp("Fire2"))
  77.         {
  78.             if(mana.GetMana() > manaCost)
  79.             {
  80.                 ApplyForceToEnemies();
  81.                 abilityReady = false;
  82.                 GetComponentInParent<PlayerMana>().ReduceMana(manaCost);
  83.                 InstantiateParticleEffect();
  84.  
  85.                 StartCoroutine(ReenableAbility());
  86.             }
  87.             else
  88.             {
  89.                 ReenableAbility();
  90.             }
  91.         }
  92.        
  93.     }
  94.  
  95.     //##########################################################################
  96.     private void ApplyForceToEnemies()
  97.     {
  98.         foreach (GameObject enemy in enemies)
  99.         {
  100.             float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
  101.             if (distanceToEnemy < radius)
  102.             {
  103.                 enemy.GetComponent<EnemyAI>().isPushed = true;
  104.  
  105.                 NavMeshAgent navMesh = enemy.GetComponent<NavMeshAgent>();
  106.                 navMesh.isStopped = true;
  107.                 navMesh.updateRotation = false;
  108.                 direction = (transform.position - enemy.transform.position) * -1f;
  109.                 navMesh.velocity = direction * (force / (distanceToEnemy * 2f));
  110.                 navMesh.speed = force / (distanceToEnemy * 2f);
  111.                 navMesh.angularSpeed = 0;
  112.                 navMesh.acceleration = force * (distanceToEnemy * 2f);
  113.  
  114.                 StartCoroutine(ResetNavMeshValues(navMesh, enemy));
  115.             }
  116.         }
  117.     }
  118.    
  119.     //##########################################################################
  120.     IEnumerator ResetNavMeshValues(NavMeshAgent navMesh, GameObject enemy)
  121.     {
  122.         yield return new WaitForSeconds(cooldown);
  123.        
  124.         enemy.GetComponent<EnemyAI>().isPushed = false;
  125.         navMesh.isStopped = false;
  126.         navMesh.updateRotation = true;
  127.         navMesh.speed = 3.5f;
  128.         navMesh.angularSpeed = 20;
  129.         navMesh.acceleration = 50;
  130.     }
  131.  
  132.     //##########################################################################
  133.     IEnumerator ReenableAbility()
  134.     {
  135.         yield return new WaitForSeconds(cooldown);
  136.        
  137.         force = 0f;
  138.         radius = 0f;
  139.         abilityReady = true;
  140.     }
  141.  
  142.     //##########################################################################
  143.     private void InstantiateParticleEffect()
  144.     {
  145.         ParticleSystem particles = Instantiate(forcePushCast, transform.position, Quaternion.identity);
  146.         particles.transform.localScale = new Vector3(radius / 10f, radius / 10f, radius / 10f);
  147.         particles.transform.parent = runtimeDump;
  148.     }
  149.  
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top