Facebook
From James Hawkins, 2 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 38
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Pathfinding;
  5. using System;
  6.  
  7. public class BasicEnemy : MonoBehaviour
  8. {
  9.     //Refs
  10.     [SerializeField] Collider2D _collider2D;
  11.  
  12.     [Header("Pathfinding")]
  13.     public Transform target;
  14.     public float activateDistance = 1;
  15.     [Range(.75f, 2f)] public float pathUpdateInSeconds = 1f;
  16.  
  17.     [Header("Physics")]
  18.     public float speed = 200f;
  19.     public float nextWayPointDistance = 1f;
  20.     public float jumpNodeHeightRequirement = 0.8f;
  21.     public float jumpModifier = .3f;
  22.     public float jumpCheckOffset = .1f;
  23.  
  24.     //ToDo: Make all of the following Customizables a Scriptable Object
  25.     [Header("Customizable")]
  26.     public bool jumpEnabled = true;
  27.     public bool followEnabled = true;
  28.     public bool directionLookEnabled = true;
  29.  
  30.     private Path path;
  31.     private int currentWayPoint = 0;
  32.     private bool isGrounded = false;
  33.     bool isTargetInDistanceFlag = false;
  34.     private float pathUpdateTimer = 1f;
  35.  
  36.     [SerializeField, HideInInspector()] private Seeker seeker;
  37.     [SerializeField, HideInInspector()] private Rigidbody2D _rb;
  38.     private void Start() { pathUpdateTimer = pathUpdateInSeconds; }
  39.  
  40.     private void Update()
  41.     {
  42.         isTargetInDistanceFlag = false;
  43.  
  44.         if (PauseManager.paused) { return; }
  45.  
  46.         isTargetInDistanceFlag = TargetInDistance() && followEnabled;
  47.  
  48.         if (pathUpdateTimer > 0) { pathUpdateTimer -= Time.deltaTime; }
  49.         else
  50.         {
  51.             UpdatePath();
  52.             pathUpdateTimer = pathUpdateInSeconds;
  53.         }
  54.     }
  55.  
  56.     private void FixedUpdate()
  57.     {
  58.         if (PauseManager.paused) { return; }
  59.         if (isTargetInDistanceFlag) PathFollow();
  60.     }
  61.  
  62.     private void PathFollow()
  63.     {
  64.         if (path == null) { return; }
  65.  
  66.         //Reached end of Path?
  67.         if (currentWayPoint >= path.vectorPath.Count) { return; }
  68.  
  69.         // See if colliding with anything
  70.         Vector3 startOffset = transform.position - new Vector3(0f, _collider2D.bounds.extents.y + jumpCheckOffset);
  71.         isGrounded = Physics2D.Raycast(startOffset, -Vector3.up, 0.05f);
  72.  
  73.         // Direction Calculation
  74.         Vector2 direction = ((Vector2)path.vectorPath[currentWayPoint] - _rb.position).normalized;
  75.         Vector2 force = direction * speed * Time.fixedDeltaTime;
  76.  
  77.         if (jumpEnabled && isGrounded)
  78.         {
  79.             if (direction.y > jumpNodeHeightRequirement)
  80.             {
  81.                 _rb.AddForce(Vector2.up * jumpModifier * speed * 2);
  82.             }
  83.         }
  84.  
  85.         // Movement Horizontal
  86.         _rb.AddForce(force * _rb.mass);
  87.  
  88.         // Next Waypoint
  89.         float distance = Vector2.Distance(_rb.position, path.vectorPath[currentWayPoint]);
  90.         if (distance < nextWayPointDistance) { currentWayPoint++; }
  91.  
  92.         // Direction Graphics Handling
  93.         if (directionLookEnabled)
  94.         {
  95.             if (_rb.velocity.x < 0.05f)
  96.             {
  97.                 transform.localScale = new Vector3(-1f * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
  98.             }
  99.             else if (_rb.velocity.x > -0.05f)
  100.             {
  101.                 transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
  102.             }
  103.         }
  104.     }
  105.  
  106.     private void OnCollisionEnter2D(Collision2D collision)
  107.     {
  108.         if (collision.gameObject.CompareTag("Player"))
  109.         {
  110.             var necromancer = collision.gameObject.GetComponent<PlayerNecromancerController>();
  111.  
  112.             if (necromancer.alive) { GameManager.Instance.ResetScene(); }
  113.         }
  114.     }
  115.  
  116.     private bool TargetInDistance() { return Vector2.Distance(transform.position, target.transform.position) < activateDistance; }
  117.  
  118.     private void UpdatePath()
  119.     {
  120.         if (TargetInDistance() && followEnabled && seeker.IsDone())
  121.         {
  122.             seeker.StartPath(_rb.position, target.position, OnPathComplete);
  123.         }
  124.     }
  125.  
  126.     private void OnPathComplete(Path p)
  127.     {
  128.         if (!p.error)
  129.         {
  130.             path = p;
  131.             currentWayPoint = 0;
  132.         }
  133.     }
  134.  
  135.     private void OnValidate()
  136.     {
  137.         if (_rb == null) { _rb = GetComponent<Rigidbody2D>(); }
  138.         if (seeker == null) { seeker = GetComponent<Seeker>(); }
  139.         if (_collider2D == null) { _collider2D = GetComponent<Collider2D>(); }
  140.     }
  141.  
  142. }
  143.  
captcha