Advertisement
EgonMilanVotrubec

SoldierLook

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