EgonMilanVotrubec

Teamup.cs

Jun 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.82 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine.AI;
  3. using UnityEngine;
  4.  
  5.  
  6. #if MILAN
  7. public class Solidertarget : MonoBehaviour
  8. {
  9.     public float health;
  10. }
  11.  
  12. public class SoliderFirering : MonoBehaviour
  13. {
  14.     public bool FireNow;
  15.     public bool isreloading;
  16. }
  17.  
  18. public class Gunscript : MonoBehaviour
  19. {
  20.     public class Instance { public bool playershoot; }
  21.     public static Instance instance;
  22. }
  23. #endif
  24.  
  25. public class Teamup : MonoBehaviour, IAgent
  26. {
  27.  
  28.     public static Teamup instance;
  29.  
  30.     public AgentType AgentType => AgentType.Enemy;
  31.  
  32.     public Vector3 Position => this.transform.localPosition;
  33.  
  34.     [HideInInspector]
  35.     public Transform Target;
  36.  
  37.     [Header ( "NAME THE OPPOSITE TEAM TAG" )]
  38.     public string OppositeTeamTag = "player";
  39.  
  40.     [Header ( "AT WHAT DISTANCE YOU WANT THIS ENEMY TO FOLLOW PLAYER " )]
  41.     public int distance = 30;
  42.     private int distanceSqr;
  43.  
  44.     [Header ( "DRAG THE NAV MESH AGENT THAT YOU CREATED ON THIS ENEMY" )]
  45.     public NavMeshAgent agent;
  46.  
  47.     // Private Variables
  48.     private Animator anim;
  49.     bool isDead = false;
  50.     private Solidertarget st;
  51.  
  52.     [Header ( "ENTER YOUR ANIMATION NAMES" )]
  53.     public string Run = "Run";
  54.     public string idlefire = "IdleFire";
  55.     public string idle = "idle";
  56.     public string Reload = "Reload";
  57.     public string Patrolling = "Walk";
  58.  
  59.     [Header ( "DRAG SOLDIER FIRING SCRIPT ATTACHED TO THIS GAMEOBJECT CHILD" )]
  60.     public SoliderFirering SoldierFiringScript;
  61.  
  62.     bool Soldierfire = false;
  63.  
  64.     [HideInInspector]
  65.     public float MyAgentSpeed;
  66.  
  67.     [Header ( "LOOKING ROTATION SPEED AND THE ENEMY EYES" )]
  68.     public float rotationSpeed = 3f;
  69.     public bool EnemyEyes = false;
  70.     public int eyesAngles = 60;
  71.  
  72.     [Header ( "IF TRUE THAN ENEMY STARTS PATROLLING" )]
  73.     public bool IsPatrolling = false;
  74.     public float PatrollingSpeed;
  75.     public Transform [ ] RandompointsForPetrolling;
  76.     bool ChangeRandompoint = false;
  77.  
  78.     int Randomise;
  79.     float angle;
  80.     bool OnceActivation = false;
  81.  
  82.     public FindNearestEnemy FNE;
  83.  
  84.     private void Awake ( )
  85.     {
  86.         if ( instance == null )
  87.         {
  88.             instance = this;
  89.         }
  90.     }
  91.  
  92.     void Start ( )
  93.     {
  94.  
  95.         st = GetComponent<Solidertarget> ( );
  96.         anim = GetComponentInChildren<Animator> ( );
  97.         agent = GetComponent<NavMeshAgent> ( );
  98.         MyAgentSpeed = agent.speed;
  99.         distanceSqr = distance * distance;
  100.     }
  101.  
  102.     void Update ( )
  103.     {
  104.         if ( FNE.NearestTarget == true )
  105.         {
  106.             if ( Target != null )
  107.             {
  108.                 Vector3 dir = Target.position - transform.position;
  109.                 if ( EnemyEyes )
  110.                     angle = Vector3.Angle ( dir, transform.forward );
  111.  
  112.                 if ( !Soldierfire )
  113.                 {
  114.                     if ( dir.sqrMagnitude < distanceSqr && isDead == false && angle < eyesAngles )
  115.                     {
  116.                         if ( Gunscript.instance != null )
  117.                             Gunscript.instance.playershoot = true;
  118.  
  119.                         dir.y = 0;
  120.  
  121.                         //this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(dir), 0.2f);
  122.                         var newRotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( dir ), rotationSpeed * Time.deltaTime ).eulerAngles;
  123.                         newRotation.x = 0;
  124.                         newRotation.z = 0;
  125.                         transform.rotation = Quaternion.Euler ( newRotation );
  126.  
  127.                         anim.SetBool ( idle, false );
  128.  
  129.                         if ( dir.sqrMagnitude > 16 )
  130.                         {
  131.                             if ( SoldierFiringScript.isreloading == false )
  132.                             {
  133.                                 agent.destination = Target.position;
  134.                                 agent.isStopped = false;
  135.                                 this.transform.Translate ( 0, 0, 0.01f );
  136.                                 anim.SetBool ( Run, true );
  137.                                 agent.speed = MyAgentSpeed;
  138.                                 anim.SetBool ( idlefire, false );
  139.                                 anim.SetBool ( Reload, false );
  140.                                 anim.SetBool ( idle, false );
  141.                                 SoldierFiringScript.FireNow = false;
  142.                             }
  143.                         }
  144.                         else
  145.                         {
  146.                             if ( SoldierFiringScript.isreloading == false )
  147.                             {
  148.                                 agent.isStopped = true;
  149.                                 anim.SetBool ( Run, false );
  150.                                 anim.SetBool ( idlefire, true );
  151.                                 anim.SetBool ( Reload, false );
  152.                                 anim.SetBool ( idle, false );
  153.                                 SoldierFiringScript.FireNow = true;
  154.                             }
  155.                             else
  156.                             {
  157.                                 agent.isStopped = true;
  158.                                 anim.SetBool ( Run, false );
  159.                                 anim.SetBool ( Reload, true );
  160.                                 agent.speed = 0;
  161.                                 anim.SetBool ( idlefire, false );
  162.                                 anim.SetBool ( idle, false );
  163.                                 agent.isStopped = true;
  164.                                 SoldierFiringScript.FireNow = false;
  165.                             }
  166.                         }
  167.                     }
  168.                     else
  169.                     {
  170.                         if ( IsPatrolling == false )
  171.                         {
  172.                             agent.isStopped = false;
  173.                             anim.SetBool ( idle, true );
  174.                             anim.SetBool ( idlefire, false );
  175.                             anim.SetBool ( Run, false );
  176.                             anim.SetBool ( Reload, false );
  177.                             SoldierFiringScript.FireNow = false;
  178.                         }
  179.                         else
  180.                         {
  181.                             if ( ChangeRandompoint == false )
  182.                             {
  183.                                 Randomise = Random.Range ( 0, RandompointsForPetrolling.Length );
  184.                                 agent.destination = RandompointsForPetrolling [ Randomise ].transform.position;
  185.                                 agent.isStopped = false;
  186.                                 this.transform.Translate ( 0, 0, 0.01f );
  187.                                 agent.speed = PatrollingSpeed;
  188.                                 anim.SetBool ( idle, false );
  189.                                 anim.SetBool ( idlefire, false );
  190.                                 anim.SetBool ( Run, false );
  191.                                 anim.SetBool ( Reload, false );
  192.                                 anim.SetBool ( Patrolling, true );
  193.                                 SoldierFiringScript.FireNow = false;
  194.                                 StartCoroutine ( WaitForCertainDestination ( ) );
  195.  
  196.                             }
  197.                         }
  198.                     }
  199.  
  200.                     if ( st.health <= 0f )
  201.                     {
  202.                         isDead = true;
  203.                         agent.isStopped = false;
  204.                         agent.isStopped = true;
  205.                     }
  206.  
  207.                 }
  208.                 else
  209.                 {
  210.                     agent.isStopped = true;
  211.                     anim.SetBool ( Run, false );
  212.                     anim.SetBool ( idlefire, true );
  213.                     anim.SetBool ( Reload, false );
  214.                     anim.SetBool ( idle, false );
  215.                     SoldierFiringScript.FireNow = true;
  216.                 }
  217.  
  218.  
  219.                 if ( Gunscript.instance?.playershoot == true )
  220.                 {
  221.                     if ( OnceActivation == false )
  222.                     {
  223.                         anim.SetBool ( idle, false );
  224.                         anim.SetBool ( idlefire, false );
  225.                         anim.SetBool ( Run, true );
  226.                         anim.SetBool ( Reload, false );
  227.                         anim.SetBool ( Patrolling, false );
  228.                         distance = 20;
  229.                         OnceActivation = true;
  230.                     }
  231.                 }
  232.  
  233.             }
  234.  
  235.         }
  236.         else
  237.         {
  238.             if ( IsPatrolling == false )
  239.             {
  240.                 agent.isStopped = false;
  241.                 anim.SetBool ( idle, true );
  242.                 anim.SetBool ( idlefire, false );
  243.                 anim.SetBool ( Run, false );
  244.                 anim.SetBool ( Reload, false );
  245.                 SoldierFiringScript.FireNow = false;
  246.             }
  247.             else
  248.             {
  249.                 if ( ChangeRandompoint == false )
  250.                 {
  251.                     Randomise = Random.Range ( 0, RandompointsForPetrolling.Length );
  252.                     agent.destination = RandompointsForPetrolling [ Randomise ].transform.position;
  253.                     agent.isStopped = false;
  254.                     transform.Translate ( 0, 0, 0.01f );
  255.                     agent.speed = PatrollingSpeed;
  256.                     anim.SetBool ( idle, false );
  257.                     anim.SetBool ( idlefire, false );
  258.                     anim.SetBool ( Run, false );
  259.                     anim.SetBool ( Reload, false );
  260.                     anim.SetBool ( Patrolling, true );
  261.                     SoldierFiringScript.FireNow = false;
  262.                     StartCoroutine ( WaitForCertainDestination ( ) );
  263.  
  264.                 }
  265.             }
  266.         }
  267.  
  268.     }
  269.  
  270.     IEnumerator WaitForCertainDestination ( )
  271.     {
  272.         ChangeRandompoint = true;
  273.         yield return new WaitForSeconds ( 3f );
  274.         ChangeRandompoint = false;
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment