Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class soliderlook : MonoBehaviour
- {
- public static soliderlook instance;
- [Header ( "DRAG THE TARGET WHICH ENEMY WANTS TO LOOK" )]
- public Transform player;
- [Header ( "AT WHAT DISTANCE YOU WANT THIS ENEMY TO FOLLOW PLAYER " )]
- public int distance = 30;
- public int AtWhatdistanceEnemyToStop = 4;
- public int AtWhatdistanceEnemyToStopMax = 8;
- [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 PARAMETER NAMES" )]
- public string Run = "Run";
- public string idlefire = "IdleFire";
- public string idle = "idle";
- public string Reload = "Reload";
- public string Patrolling = "Walk";
- public string CrouchingFire = "CrouchingIdle";
- public string CrouchingReload = "CrouchingReload";
- public string CrouchingWalking = "CrouchingWalking";
- public string Sprinting = "Sprinting";
- // public string WalkingLeft = "Left";
- // public string WalkingRight = "Right";
- // public float ChangeFiringMovements = 2f;
- [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 float TimeToChangePatrollingPoint;
- public bool MyOwnRandompoints = false;
- public Transform [ ] RandompointsForPetrolling;
- [Header ( "COVER SYSTEM" )]
- public bool IsTakingCover = false;
- public bool ReachedHidingPoint = false;
- int Randomise;
- float angle;
- bool OnceActivation = false;
- [HideInInspector]
- public Vector3 Target;
- float Timer;
- float SaveTime;
- [HideInInspector]
- public GameObject DetermineCrouchingPositon;
- bool CheckAiReached = false;
- int StoreDistance;
- [HideInInspector]
- public bool RemoveCover = false;
- private void Awake ( )
- {
- if ( instance == null )
- {
- instance = this;
- }
- }
- void Start ( )
- {
- st = GetComponent<Solidertarget> ( );
- anim = GetComponentInChildren<Animator> ( );
- agent = GetComponent<NavMeshAgent> ( );
- SaveTime = TimeToChangePatrollingPoint;
- MyAgentSpeed = agent.speed;
- TimeToChangePatrollingPoint = Timer;
- StartCoroutine ( ChangeDestination ( ) );
- StoreDistance = AtWhatdistanceEnemyToStop;
- }
- void SetAnims( string animName = null )
- {
- anim.SetBool ( Run, false );
- anim.SetBool ( idlefire, false );
- anim.SetBool ( Reload, false );
- anim.SetBool ( idle, false );
- anim.SetBool ( Sprinting, false );
- if ( animName != null )
- anim.SetBool ( animName, true );
- }
- void FixedUpdate ( )
- {
- Timer += Time.deltaTime;
- Vector3 dir = player.position - this.transform.position;
- if ( EnemyEyes == true )
- {
- angle = Vector3.Angle ( dir, this.transform.forward );
- }
- if ( Soldierfire )
- {
- var newRotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( player.position - transform.position ), rotationSpeed * Time.deltaTime ).eulerAngles;
- newRotation.x = 0;
- newRotation.z = 0;
- transform.rotation = Quaternion.Euler ( newRotation );
- agent.isStopped = true;
- SetAnims ( idlefire );
- // anim.SetBool(CrouchingFire, false);
- SoldierFiringScript.FireNow = true;
- return;
- }
- if ( Vector3.Distance ( player.position, this.transform.position ) < distance && isDead == false && angle < eyesAngles )
- {
- if ( Gunscript.instance != null )
- Gunscript.instance.playershoot = true;
- dir.y = 0;
- anim.SetBool ( idle, false );
- Debug.Log ( dir.magnitude );
- if ( dir.magnitude > AtWhatdistanceEnemyToStop )
- {
- RemoveCover = true;
- var newRotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( player.position - transform.position ), rotationSpeed * Time.deltaTime ).eulerAngles;
- newRotation.x = 0;
- newRotation.z = 0;
- transform.rotation = Quaternion.Euler ( newRotation );
- if ( !SoldierFiringScript.isreloading )
- {
- ReachedHidingPoint = false;
- agent.destination = player.position;
- this.transform.Translate ( 0, 0, 0.01f );
- agent.speed = MyAgentSpeed;
- agent.isStopped = false;
- SetAnims ( Run );
- anim.SetBool ( CrouchingFire, false );
- anim.SetBool ( CrouchingReload, false );
- }
- else
- {
- agent.speed = 0;
- agent.isStopped = true;
- SetAnims ( Reload );
- }
- SoldierFiringScript.FireNow = false;
- }
- else
- {
- RemoveCover = false;
- if ( !IsTakingCover )
- {
- var newRotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( player.position - transform.position ), rotationSpeed * Time.deltaTime ).eulerAngles;
- newRotation.x = newRotation.z = 0;
- transform.rotation = Quaternion.Euler ( newRotation );
- agent.isStopped = true;
- if ( !SoldierFiringScript.isreloading )
- {
- SetAnims ( idlefire );
- SoldierFiringScript.FireNow = true;
- }
- else
- {
- SetAnims ( Reload );
- SoldierFiringScript.FireNow = false;
- }
- }
- else
- {
- var rotationDelta = SoldierFiringScript.isreloading ? rotationSpeed * Time.deltaTime : 15f * Time.deltaTime;
- var lookAt = ReachedHidingPoint ? player.position : DetermineCrouchingPositon.transform.position;
- var newRotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( lookAt - transform.position ), rotationDelta ).eulerAngles;
- newRotation.x = newRotation.z = 0;
- transform.rotation = Quaternion.Euler ( newRotation );
- if ( !ReachedHidingPoint )
- {
- agent.destination = DetermineCrouchingPositon.transform.position;
- agent.isStopped = false;
- agent.speed = MyAgentSpeed;
- transform.Translate ( 0, 0, 0.01f );
- SetAnims ( Sprinting );
- SoldierFiringScript.FireNow = false;
- }
- else
- {
- agent.isStopped = true;
- agent.speed = 0f;
- SetAnims ( );
- anim.SetBool ( CrouchingFire, true );
- anim.SetBool ( CrouchingReload, false );
- SoldierFiringScript.FireNow = true;
- }
- }
- }
- }
- else
- {
- if ( IsPatrolling == false )
- {
- agent.isStopped = false;
- SetAnims ( idle );
- SoldierFiringScript.FireNow = false;
- }
- else
- {
- if ( Timer >= TimeToChangePatrollingPoint )
- {
- if ( MyOwnRandompoints == true )
- {
- CreateRandomDestiationForPetrolling ( );
- agent.destination = RandompointsForPetrolling [ Randomise ].transform.position;
- }
- else
- {
- NewTargets ( );
- }
- agent.isStopped = false;
- this.transform.Translate ( 0, 0, 0.01f );
- agent.speed = PatrollingSpeed;
- SetAnims ( Patrolling );
- SoldierFiringScript.FireNow = false;
- Timer = 0;
- }
- }
- }
- if ( st.health <= 0f )
- {
- isDead = true;
- agent.isStopped = isDead ;
- }
- }
- public void CreateRandomDestiationForPetrolling ( )
- {
- Randomise = Random.Range ( 0, RandompointsForPetrolling.Length );
- }
- public void NewTargets ( )
- {
- float myX = transform.position.x;
- float myZ = transform.position.z;
- float Xpos = myX + Random.Range ( myX - 100f, myX + 100f );
- float zpos = myZ + Random.Range ( myZ - 100f, myZ + 100f );
- Target = new Vector3 ( Xpos, gameObject.transform.position.y, zpos );
- agent.SetDestination ( Target );
- }
- IEnumerator ChangeDestination ( )
- {
- yield return new WaitForSeconds ( 1f );
- TimeToChangePatrollingPoint = SaveTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement