Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class ForcePush : MonoBehaviour
- {
- Vector3 direction;
- [SerializeField] float force;
- [SerializeField] float maxForce;
- [SerializeField] float forceBuildUpAmount;
- [SerializeField] float radius;
- [SerializeField] float radiusBuildUpAmount;
- PlayerMana mana;
- [SerializeField] float manaCost;
- [SerializeField] float manaBuildUpAmount;
- [SerializeField] float cooldown;
- bool abilityReady;
- List<GameObject> enemies = new List<GameObject>();
- [SerializeField] ParticleSystem forcePushCast;
- Transform runtimeDump;
- //##########################################################################
- private void Start()
- {
- runtimeDump = GameObject.Find("RuntimeDump").transform;
- abilityReady = true;
- mana = GetComponentInParent<PlayerMana>();
- foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy")) // add boss!?
- {
- enemies.Add(enemy);
- }
- }
- //##########################################################################
- private void FixedUpdate()
- {
- if(abilityReady)
- {
- BuildUp();
- Cast();
- }
- }
- //##########################################################################
- private void BuildUp()
- {
- if(Input.GetButtonDown("Fire2"))
- {
- manaCost = 0f;
- }
- if (Input.GetButton("Fire2"))
- {
- if (force < maxForce)
- {
- force += forceBuildUpAmount * Time.deltaTime;
- radius += radiusBuildUpAmount * Time.deltaTime;
- manaCost += manaBuildUpAmount * Time.deltaTime;
- }
- }
- }
- //##########################################################################
- private void Cast()
- {
- if (Input.GetButtonUp("Fire2"))
- {
- if(mana.GetMana() > manaCost)
- {
- ApplyForceToEnemies();
- abilityReady = false;
- GetComponentInParent<PlayerMana>().ReduceMana(manaCost);
- InstantiateParticleEffect();
- StartCoroutine(ReenableAbility());
- }
- else
- {
- ReenableAbility();
- }
- }
- }
- //##########################################################################
- private void ApplyForceToEnemies()
- {
- foreach (GameObject enemy in enemies)
- {
- float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
- if (distanceToEnemy < radius)
- {
- enemy.GetComponent<EnemyAI>().isPushed = true;
- NavMeshAgent navMesh = enemy.GetComponent<NavMeshAgent>();
- navMesh.isStopped = true;
- navMesh.updateRotation = false;
- direction = (transform.position - enemy.transform.position) * -1f;
- navMesh.velocity = direction * (force / (distanceToEnemy * 2f));
- navMesh.speed = force / (distanceToEnemy * 2f);
- navMesh.angularSpeed = 0;
- navMesh.acceleration = force * (distanceToEnemy * 2f);
- StartCoroutine(ResetNavMeshValues(navMesh, enemy));
- }
- }
- }
- //##########################################################################
- IEnumerator ResetNavMeshValues(NavMeshAgent navMesh, GameObject enemy)
- {
- yield return new WaitForSeconds(cooldown);
- enemy.GetComponent<EnemyAI>().isPushed = false;
- navMesh.isStopped = false;
- navMesh.updateRotation = true;
- navMesh.speed = 3.5f;
- navMesh.angularSpeed = 20;
- navMesh.acceleration = 50;
- }
- //##########################################################################
- IEnumerator ReenableAbility()
- {
- yield return new WaitForSeconds(cooldown);
- force = 0f;
- radius = 0f;
- abilityReady = true;
- }
- //##########################################################################
- private void InstantiateParticleEffect()
- {
- ParticleSystem particles = Instantiate(forcePushCast, transform.position, Quaternion.identity);
- particles.transform.localScale = new Vector3(radius / 10f, radius / 10f, radius / 10f);
- particles.transform.parent = runtimeDump;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement