Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class EnemyAIRanged : Bolt.EntityEventListener<IIonCanonSpider>
- {
- [SerializeField]
- GameObject ionBulletSpawnPoint;
- [SerializeField]
- GameObject ionBulletPrefab;
- [SerializeField]
- float ionBulletSpeed = 200f;
- [SerializeField]
- GameObject sheelIjectionPrefab;
- float muzzleFlashTimer;
- float muzzleFlashTimerDuration = 1f;
- Transform _target;
- bool _setNewTarget = true;
- float _timeOutTimer;
- float _timeOutTimervalue = 3;
- NavMeshAgent _agent;
- bool _canTrackPlayer = false;
- [SerializeField]
- List<GameObject> _wayPoints;
- bool _canTravelToWayPoints = true;
- private float _wayPointWaitTimer;
- private float _wayPointWaitTimerValue;
- int _nextWayPoint = 0;
- private bool _canSetTracking = true;
- // Shoot references
- public float _shootRateTimerDuration = 1f;
- private float _shootTimer;
- [SerializeField]
- Animator _anim;
- [SerializeField]
- private float _viewAngle = 45;
- [SerializeField]
- private float _attackDistance = 2;
- [SerializeField]
- private float _rangedAttackDistance = 10;
- bool _canAttack;
- float distance;
- public float wayPointStoppingDistance = 2;
- Vector3 direction;
- [SerializeField]
- GameObject muzzleFlash;
- private bool hasStopped;
- public GameObject _shellIjectionPosition;
- IBMPlayerState playerState;
- public override void Attached()
- {
- if (!entity.hasControl && !entity.isOwner)
- {
- state.SetTransforms(state.transform, transform);
- state.SetAnimator(GetComponent<Animator>());
- state.Animator.SetLayerWeight(0, 1);
- state.Animator.SetLayerWeight(1, 1);
- }
- //_target.position = Vector3.zero;
- base.Attached();
- if (BoltNetwork.IsServer)
- {
- state.SetTransforms(state.transform, transform);
- state.SetAnimator(GetComponent<Animator>());
- state.Animator.SetLayerWeight(0, 1);
- state.Animator.SetLayerWeight(1, 1);
- //state.Target = _target.transform.position;
- _anim = GetComponent<Animator>();
- state.SetAnimator(GetComponent<Animator>());
- _agent = GetComponent<NavMeshAgent>();
- _agent.stoppingDistance = wayPointStoppingDistance;
- _canTravelToWayPoints = true;
- _wayPoints.AddRange(GameObject.FindGameObjectsWithTag("EnemyWayPoints"));
- var rand = Random.Range(0, _wayPoints.Count);
- _nextWayPoint = rand;
- _agent.SetDestination(_wayPoints[rand].transform.position);
- _agent.speed = 4;
- _agent.isStopped = false;
- if (_anim != null)
- {
- _anim.speed = 1;
- _anim.SetBool("Run", false);
- _anim.SetBool("Idle", false);
- _anim.SetBool("Walk", true);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- }
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- if (BoltNetwork.IsServer)
- {
- RangedAttack();
- }
- }
- private void RangedAttack()
- {
- if (_target != null)
- {
- //state.Target = _target.position;
- DirectionIs();
- float angle = Vector3.Angle(direction, transform.forward);
- distance = Vector3.Distance(_target.position, this.transform.position);
- direction.y = 0;
- if (_target != null)
- {
- BoltEntity pEntity = _target.GetComponent<BoltEntity>();
- playerState = pEntity.GetState<IBMPlayerState>();
- }
- if (distance < 20 && angle < _viewAngle && !playerState.Dead)
- {
- _canTrackPlayer = true;
- _canTravelToWayPoints = false;
- _agent.stoppingDistance = _attackDistance;
- if (_anim != null)
- {
- _anim.SetBool("Run", false);
- _anim.SetBool("Idle", true);
- _anim.SetBool("Walk", false);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- }
- if (_canTrackPlayer)
- {
- if (playerState.Dead)
- {
- _agent.isStopped = false;
- _target = null;
- _timeOutTimer = 0;
- _setNewTarget = true;
- _canTrackPlayer = false;
- _agent.stoppingDistance = .5f;
- _agent.speed = 2f;
- _canTravelToWayPoints = true;
- _anim.speed = 1;
- state.AnimSpeed = _anim.speed;
- _agent.SetDestination(_wayPoints[_nextWayPoint].transform.position);
- if (_anim != null)
- {
- _anim.SetBool("Run", false);
- _anim.SetBool("Idle", false);
- _anim.SetBool("Walk", true);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- return;
- }
- transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.3f);
- if (distance <= _attackDistance && !playerState.Dead)
- {
- _shootTimer += Time.deltaTime;
- if (_shootTimer > _shootRateTimerDuration)
- {
- if (BoltNetwork.IsServer)
- {
- //Instantiate Bullet
- var entity = GetComponentInParent<BoltEntity>();
- var rotation = entity.GetComponent<Transform>().rotation;
- var newpos = new Vector3(muzzleFlash.transform.position.x, muzzleFlash.transform.position.y, muzzleFlash.transform.position.z + 1);
- // create shell
- var _ionBullet = BoltNetwork.Instantiate(ionBulletPrefab, ionBulletSpawnPoint.transform.position, rotation);
- _ionBullet.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, ionBulletSpeed), ForceMode.VelocityChange);
- GameObject go = BoltNetwork.Instantiate(sheelIjectionPrefab, _shellIjectionPosition.transform.position, rotation);
- go.GetComponent<Rigidbody>().AddRelativeForce(2f, 3f, -2f, ForceMode.Impulse);
- go.GetComponent<Rigidbody>().AddTorque(0, 120, 400, ForceMode.VelocityChange);
- CmdAttack();
- }
- _shootTimer = 0;
- }
- _agent.isStopped = true;
- _canAttack = true;
- _timeOutTimer = 0;
- _anim.speed = 1;
- state.AnimSpeed = _anim.speed;
- _agent.stoppingDistance = _attackDistance;
- if (_anim != null)
- {
- _anim.SetBool("Run", false);
- _anim.SetBool("Idle", true);
- _anim.SetBool("Walk", false);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- }
- if (distance < 15 && distance > _attackDistance)
- {
- _agent.isStopped = false;
- _canAttack = false;
- _agent.SetDestination(_target.position);
- _agent.speed = 8;
- _timeOutTimer = 0;
- _anim.speed = 2;
- state.AnimSpeed = _anim.speed;
- _agent.stoppingDistance = _attackDistance;
- if (_anim != null)
- {
- _anim.SetBool("Run", true);
- _anim.SetBool("Idle", false);
- _anim.SetBool("Walk", false);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- }
- else if (distance >= 15)// just changed
- {
- _timeOutTimer += Time.deltaTime;
- if (_timeOutTimer >= _timeOutTimervalue)
- {
- //_target.position = Vector3.zero;
- _agent.isStopped = false;
- _target = null;
- _timeOutTimer = 0;
- _setNewTarget = true;
- _canTrackPlayer = false;
- _agent.stoppingDistance = wayPointStoppingDistance;
- _canTravelToWayPoints = true;
- _anim.speed = 1;
- state.AnimSpeed = _anim.speed;
- _agent.SetDestination(_wayPoints[_nextWayPoint].transform.position);
- if (_anim != null)
- {
- _anim.SetBool("Run", false);
- _anim.SetBool("Idle", false);
- _anim.SetBool("Walk", true);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- }
- }
- }
- NavigateEnemy();
- }
- }
- private void MeleeWander()
- {
- }
- private void MuzzleFlash()
- {
- }
- private void NavigateEnemy()
- {
- if (_canTravelToWayPoints)
- {
- RotationDirection();
- var waypointNextDistance = Vector3.Distance(_target.position, _wayPoints[_nextWayPoint].transform.position);
- if (waypointNextDistance < wayPointStoppingDistance && !hasStopped)
- {
- Debug.Log("Starting navigation sequence!");
- hasStopped = true;
- StartCoroutine(NavigateLogic());
- }
- else if (hasStopped && waypointNextDistance > 2f)
- {
- hasStopped = false;
- }
- }
- }
- IEnumerator NavigateLogic()
- {
- hasStopped = true;
- _agent.isStopped = true;
- _agent.speed = 0;
- if (_anim != null)
- {
- _anim.SetBool("Run", false);
- _anim.SetBool("Idle", true);
- _anim.SetBool("Walk", false);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- Debug.Log("Navigating in Idle");
- yield return new WaitForSeconds(Random.Range(.5f, 2f));
- _nextWayPoint = Random.Range(0, _wayPoints.Count);
- _wayPointWaitTimerValue = Random.Range(.5f, 2f);
- //RandomNavmeshLocation(4f);
- _agent.SetDestination(_wayPoints[_nextWayPoint].transform.position);
- _wayPointWaitTimer = 0;
- _agent.isStopped = false;
- _agent.speed = 4;
- if (_anim != null)
- {
- _anim.SetBool("Run", false);
- _anim.SetBool("Idle", false);
- _anim.SetBool("Walk", true);
- state.Idle = _anim.GetBool("Idle");
- state.Walk = _anim.GetBool("Walk");
- state.Run = _anim.GetBool("Run");
- }
- Debug.Log("Going to Way Point");
- }
- private void DirectionIs()
- {
- direction = _target.position - this.transform.position;
- }
- private void RotateToAttacker()
- {
- Vector3 newDirection = _target.position - transform.position;
- newDirection.y = 0;
- if (newDirection != Vector3.zero)
- {
- transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(newDirection), 0.3f);
- }
- }
- private void RotationDirection()
- {
- Vector3 newDirection = _wayPoints[_nextWayPoint].transform.position - transform.position;
- newDirection.y = 0;
- if (newDirection != Vector3.zero)
- {
- if (transform.forward != Vector3.zero)
- {
- transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(transform.forward), Time.deltaTime * 0.3f
- );
- }
- }
- }
- //[Command] ------------------Update
- private void CmdAttack()
- {
- if (BoltNetwork.IsServer)
- {
- muzzleFlash.SetActive(true);
- Invoke("ResetFlash", .1f);
- Debug.Log("Fired and should of flashed");
- }
- }
- private void ResetFlash()
- {
- if (BoltNetwork.IsServer)
- {
- muzzleFlash.SetActive(false);
- }
- }
- public bool CanAttack()
- {
- return _canAttack;
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.tag == "Player")
- {
- if (_setNewTarget)
- {
- _target = other.gameObject.transform;
- //state.Target = _target.transform.position;
- playerState = _target.GetComponent<IBMPlayerState>();
- _setNewTarget = false;
- }
- }
- }
- private void OnTriggerStay(Collider other)
- {
- // Debug.Log("ENTERED ONTRIGGER: " + other.gameObject.name.ToString());
- if (_target == null && other.gameObject.tag == "Player")
- {
- _target = other.gameObject.transform;
- //state.Target = _target.transform.position;
- playerState = _target.GetComponent<IBMPlayerState>();
- _setNewTarget = false;
- }
- }
- public void DestroyMe()
- {
- if (BoltNetwork.IsServer)
- {
- BoltNetwork.Destroy(entity.gameObject);
- }
- }
- //Wandom random point
- public Vector3 RandomNavmeshLocation(float radius)
- {
- Vector3 randomDirection = Random.insideUnitSphere * radius;
- randomDirection += transform.position;
- NavMeshHit hit;
- Vector3 finalPosition = Vector3.zero;
- if (NavMesh.SamplePosition(randomDirection, out hit, radius, 1))
- {
- finalPosition = hit.position;
- }
- return finalPosition;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement