Advertisement
johnnygoodguy2000

EnemyMovement.cs

Jun 15th, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemyMovement : MonoBehaviour
  6. {
  7.     [SerializeField] private float speed;
  8.     [SerializeField] private float rotationSpeed;
  9.     [SerializeField] private float screenBorder;
  10.  
  11.     private Rigidbody2D rigidbody;
  12.     private PlayerAwarenessController playerAwarenessController;
  13.  
  14.     private Vector2 targetDirection;
  15.  
  16.     private float changeDirectionCooldown;
  17.     private Camera camera;
  18.  
  19.     private void Awake()
  20.     {
  21.         rigidbody = GetComponent<Rigidbody2D>();
  22.         playerAwarenessController = GetComponent<PlayerAwarenessController>();
  23.  
  24.         targetDirection = transform.up;
  25.  
  26.         camera = Camera.main;
  27.     }
  28.  
  29.     private void FixedUpdate()
  30.     {
  31.         UpdateTargetDirection();
  32.         RotateTowardsTarget();
  33.         SetVelocity();
  34.     }
  35.  
  36.     private void UpdateTargetDirection()
  37.     {
  38.         HandleRandomDirectionChange();
  39.         HandlePlayerTargeting();
  40.         HandleEnemyOffScreen();
  41.     }
  42.  
  43.     private void HandleRandomDirectionChange()
  44.     {
  45.         changeDirectionCooldown -= Time.deltaTime;
  46.  
  47.         if (changeDirectionCooldown <= 0)
  48.         {
  49.             float angleChange = Random.Range(-90f, 90f);
  50.             UnityEngine.Quaternion rotation = UnityEngine.Quaternion.Euler(0, 0, angleChange);
  51.  
  52.             targetDirection = rotation * targetDirection;
  53.  
  54.             changeDirectionCooldown = Random.Range(1f, 5f);
  55.         }
  56.     }
  57.  
  58.     private void HandlePlayerTargeting()
  59.     {
  60.         if (playerAwarenessController.AwareOfPlayer)
  61.         {
  62.             targetDirection = playerAwarenessController.DirectionTopPlayer;
  63.         }
  64.     }
  65.  
  66.     private void HandleEnemyOffScreen()
  67.     {
  68.  
  69.         UnityEngine.Vector2 screenPosition = camera.WorldToScreenPoint(transform.position);
  70.  
  71.         if ((screenPosition.x < screenBorder && targetDirection.x < 0) || (screenPosition.x > camera.pixelWidth - screenBorder && targetDirection.x > 0))
  72.         {
  73.             targetDirection = new UnityEngine.Vector2(-targetDirection.x, targetDirection.y);
  74.  
  75.         }
  76.          if ((screenPosition.y < screenBorder && targetDirection.y < 0) || (screenPosition.y > camera.pixelHeight - screenBorder && targetDirection.y > 0))
  77.         {
  78.             targetDirection = new UnityEngine.Vector2(targetDirection.x, -targetDirection.y);
  79.  
  80.         }
  81.  
  82.     }
  83.  
  84.     private void RotateTowardsTarget()
  85.     {
  86.         float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg - 90f;
  87.         UnityEngine.Quaternion targetRotation = UnityEngine.Quaternion.Euler(0, 0, angle);
  88.         rigidbody.MoveRotation(Mathf.LerpAngle(rigidbody.rotation, targetRotation.eulerAngles.z, rotationSpeed * Time.deltaTime));
  89.     }
  90.  
  91.     private void SetVelocity()
  92.     {
  93.         rigidbody.velocity = targetDirection * speed;
  94.     }
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement