Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine.AI;
- using UnityEngine;
- #if MILAN
- public class Solidertarget : MonoBehaviour
- {
- public float health;
- }
- public class SoliderFirering : MonoBehaviour
- {
- public bool FireNow;
- public bool isreloading;
- }
- public class Gunscript : MonoBehaviour
- {
- public class Instance { public bool playershoot; }
- public static Instance instance;
- }
- #endif
- public class Teamup : MonoBehaviour, IAgent
- {
- public static Teamup instance;
- public AgentType AgentType => AgentType.Enemy;
- public Vector3 Position => this.transform.localPosition;
- [HideInInspector]
- public Transform Target;
- [Header ( "NAME THE OPPOSITE TEAM TAG" )]
- public string OppositeTeamTag = "player";
- [Header ( "AT WHAT DISTANCE YOU WANT THIS ENEMY TO FOLLOW PLAYER " )]
- public int distance = 30;
- private int distanceSqr;
- [Header ( "DRAG THE NAV MESH AGENT THAT YOU CREATED ON THIS ENEMY" )]
- public NavMeshAgent agent;
- // Private Variables
- private Animator anim;
- bool isDead = false;
- private Solidertarget st;
- [Header ( "ENTER YOUR ANIMATION NAMES" )]
- public string Run = "Run";
- public string idlefire = "IdleFire";
- public string idle = "idle";
- public string Reload = "Reload";
- public string Patrolling = "Walk";
- [Header ( "DRAG SOLDIER FIRING SCRIPT ATTACHED TO THIS GAMEOBJECT CHILD" )]
- public SoliderFirering SoldierFiringScript;
- bool Soldierfire = false;
- [HideInInspector]
- public float MyAgentSpeed;
- [Header ( "LOOKING ROTATION SPEED AND THE ENEMY EYES" )]
- public float rotationSpeed = 3f;
- public bool EnemyEyes = false;
- public int eyesAngles = 60;
- [Header ( "IF TRUE THAN ENEMY STARTS PATROLLING" )]
- public bool IsPatrolling = false;
- public float PatrollingSpeed;
- public Transform [ ] RandompointsForPetrolling;
- bool ChangeRandompoint = false;
- int Randomise;
- float angle;
- bool OnceActivation = false;
- public FindNearestEnemy FNE;
- private void Awake ( )
- {
- if ( instance == null )
- {
- instance = this;
- }
- }
- void Start ( )
- {
- st = GetComponent<Solidertarget> ( );
- anim = GetComponentInChildren<Animator> ( );
- agent = GetComponent<NavMeshAgent> ( );
- MyAgentSpeed = agent.speed;
- distanceSqr = distance * distance;
- }
- void Update ( )
- {
- if ( FNE.NearestTarget == true )
- {
- if ( Target != null )
- {
- Vector3 dir = Target.position - transform.position;
- if ( EnemyEyes )
- angle = Vector3.Angle ( dir, transform.forward );
- if ( !Soldierfire )
- {
- if ( dir.sqrMagnitude < distanceSqr && isDead == false && angle < eyesAngles )
- {
- if ( Gunscript.instance != null )
- Gunscript.instance.playershoot = true;
- dir.y = 0;
- //this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(dir), 0.2f);
- var newRotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( dir ), rotationSpeed * Time.deltaTime ).eulerAngles;
- newRotation.x = 0;
- newRotation.z = 0;
- transform.rotation = Quaternion.Euler ( newRotation );
- anim.SetBool ( idle, false );
- if ( dir.sqrMagnitude > 16 )
- {
- if ( SoldierFiringScript.isreloading == false )
- {
- agent.destination = Target.position;
- agent.isStopped = false;
- this.transform.Translate ( 0, 0, 0.01f );
- anim.SetBool ( Run, true );
- agent.speed = MyAgentSpeed;
- anim.SetBool ( idlefire, false );
- anim.SetBool ( Reload, false );
- anim.SetBool ( idle, false );
- SoldierFiringScript.FireNow = false;
- }
- }
- else
- {
- if ( SoldierFiringScript.isreloading == false )
- {
- agent.isStopped = true;
- anim.SetBool ( Run, false );
- anim.SetBool ( idlefire, true );
- anim.SetBool ( Reload, false );
- anim.SetBool ( idle, false );
- SoldierFiringScript.FireNow = true;
- }
- else
- {
- agent.isStopped = true;
- anim.SetBool ( Run, false );
- anim.SetBool ( Reload, true );
- agent.speed = 0;
- anim.SetBool ( idlefire, false );
- anim.SetBool ( idle, false );
- agent.isStopped = true;
- SoldierFiringScript.FireNow = false;
- }
- }
- }
- else
- {
- if ( IsPatrolling == false )
- {
- agent.isStopped = false;
- anim.SetBool ( idle, true );
- anim.SetBool ( idlefire, false );
- anim.SetBool ( Run, false );
- anim.SetBool ( Reload, false );
- SoldierFiringScript.FireNow = false;
- }
- else
- {
- if ( ChangeRandompoint == false )
- {
- Randomise = Random.Range ( 0, RandompointsForPetrolling.Length );
- agent.destination = RandompointsForPetrolling [ Randomise ].transform.position;
- agent.isStopped = false;
- this.transform.Translate ( 0, 0, 0.01f );
- agent.speed = PatrollingSpeed;
- anim.SetBool ( idle, false );
- anim.SetBool ( idlefire, false );
- anim.SetBool ( Run, false );
- anim.SetBool ( Reload, false );
- anim.SetBool ( Patrolling, true );
- SoldierFiringScript.FireNow = false;
- StartCoroutine ( WaitForCertainDestination ( ) );
- }
- }
- }
- if ( st.health <= 0f )
- {
- isDead = true;
- agent.isStopped = false;
- agent.isStopped = true;
- }
- }
- else
- {
- agent.isStopped = true;
- anim.SetBool ( Run, false );
- anim.SetBool ( idlefire, true );
- anim.SetBool ( Reload, false );
- anim.SetBool ( idle, false );
- SoldierFiringScript.FireNow = true;
- }
- if ( Gunscript.instance?.playershoot == true )
- {
- if ( OnceActivation == false )
- {
- anim.SetBool ( idle, false );
- anim.SetBool ( idlefire, false );
- anim.SetBool ( Run, true );
- anim.SetBool ( Reload, false );
- anim.SetBool ( Patrolling, false );
- distance = 20;
- OnceActivation = true;
- }
- }
- }
- }
- else
- {
- if ( IsPatrolling == false )
- {
- agent.isStopped = false;
- anim.SetBool ( idle, true );
- anim.SetBool ( idlefire, false );
- anim.SetBool ( Run, false );
- anim.SetBool ( Reload, false );
- SoldierFiringScript.FireNow = false;
- }
- else
- {
- if ( ChangeRandompoint == false )
- {
- Randomise = Random.Range ( 0, RandompointsForPetrolling.Length );
- agent.destination = RandompointsForPetrolling [ Randomise ].transform.position;
- agent.isStopped = false;
- transform.Translate ( 0, 0, 0.01f );
- agent.speed = PatrollingSpeed;
- anim.SetBool ( idle, false );
- anim.SetBool ( idlefire, false );
- anim.SetBool ( Run, false );
- anim.SetBool ( Reload, false );
- anim.SetBool ( Patrolling, true );
- SoldierFiringScript.FireNow = false;
- StartCoroutine ( WaitForCertainDestination ( ) );
- }
- }
- }
- }
- IEnumerator WaitForCertainDestination ( )
- {
- ChangeRandompoint = true;
- yield return new WaitForSeconds ( 3f );
- ChangeRandompoint = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment