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;
- using System;
- public class Enemy : MonoBehaviour
- {
- [SerializeField]
- private GameObject bullet;
- [SerializeField]
- private Transform gun;
- [SerializeField]
- private float shootPower = 20000f;
- [SerializeField]
- private float targetEnemyMaxDistance = 50f;
- [SerializeField]
- private float shootDelay = 2.5f;
- [SerializeField]
- private int bulletDamage = 2;
- [SerializeField]
- private Transform[] wayPoints;
- private Transform target;
- private NavMeshAgent agent;
- private Transform player;
- private bool seeTarget;
- private Vector3 targetDirection;
- private int currentWayPoint = 0;
- public Transform Target
- {
- get
- {
- return target;
- }
- set
- {
- target = value;
- }
- }
- void Awake()
- {
- agent = GetComponent<NavMeshAgent>();
- }
- private void Start()
- {
- InvokeRepeating("Shoot", 0.0f, shootDelay);
- }
- // Update is called once per frame
- void Update()
- {
- /* if(target!= null)
- {
- agent.SetDestination(target.position);
- CheckTargetVisibility();
- }
- */
- GoByWayPoints();
- GoToPlayer();
- }
- private void Shoot()
- {
- if (seeTarget)
- {
- GameObject newBullet = Instantiate(bullet, gun.position, gun.rotation);
- if (targetDirection != null)
- {
- targetDirection.Normalize();
- newBullet.GetComponent<Rigidbody>().AddForce(targetDirection * shootPower);
- }
- newBullet.GetComponent<Damager>().Damage = bulletDamage;
- Destroy(newBullet, 5);
- }
- }
- private void CheckTargetVisibility()
- {
- RaycastHit hit;
- targetDirection = target.position - gun.position;
- Ray ray = new Ray(gun.position, targetDirection);
- if (Physics.Raycast(ray, out hit) && hit.transform == target)
- {
- seeTarget = true;
- return;
- }
- seeTarget = false;
- }
- private void GoByWayPoints()
- {
- float minDistance = 50f;
- if (wayPoints.Length > 0 )
- {
- try
- {
- player = GameObject.FindGameObjectWithTag("Player").transform;
- }
- catch(NullReferenceException e)
- {
- return;
- }
- if (player != null )
- {
- if((player.position - transform.position).magnitude < targetEnemyMaxDistance)
- {
- target = player;
- agent.SetDestination(target.position);
- CheckTargetVisibility();
- Debug.Log("GoToPlayer()");
- target = null;
- }
- else
- {
- if (target == null || (transform.position - target.position).magnitude <= minDistance)
- {
- seeTarget = false;
- target = wayPoints[currentWayPoint];
- agent.SetDestination(target.position);
- currentWayPoint++;
- Debug.Log(currentWayPoint);
- if (currentWayPoint >= wayPoints.Length)
- currentWayPoint = 0;
- Debug.Log("GoWayPoints()");
- }
- }
- }
- }
- }
- private void GoToPlayer()
- {
- try
- {
- player = GameObject.FindGameObjectWithTag("Player").transform;
- }
- catch (NullReferenceException e)
- {
- Debug.Log("null");
- return;
- }
- if (target != null && target == player)
- {
- agent.SetDestination(target.position);
- CheckTargetVisibility();
- Debug.Log("GoToPlayer()");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement