Advertisement
evelynshilosky

AI_Movement - Part 1

Jan 10th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class AI_Movement : MonoBehaviour
  7. {
  8.  
  9.     Animator animator;
  10.  
  11.     public float moveSpeed = 0.2f;
  12.  
  13.     Vector3 stopPosition;
  14.  
  15.     float walkTime;
  16.     public float walkCounter;
  17.     public int walkTimeMin = 3;
  18.     public int walkTimeMax = 6;
  19.     float waitTime;
  20.     public float waitCounter;
  21.  
  22.     int WalkDirection;
  23.  
  24.     public bool isWalking;
  25.  
  26.     // Object Detection Variables (Hopefully this will work)
  27.     public float scanDistance = 5f;
  28.     public float rotationIncrement = 45f; // Degree increment for rotation checks
  29.  
  30.     // Start is called before the first frame update
  31.     void Start()
  32.     {
  33.         animator = GetComponent<Animator>();
  34.  
  35.         //So that all the prefabs don't move/stop at the same time
  36.         walkTime = Random.Range(walkTimeMin, walkTimeMax);
  37.         waitTime = Random.Range(5, 7);
  38.  
  39.  
  40.         waitCounter = waitTime;
  41.         walkCounter = walkTime;
  42.  
  43.         ChooseDirection();
  44.     }
  45.  
  46.     // Update is called once per frame
  47.     void Update()
  48.     {
  49.         if (isWalking)
  50.         {
  51.             // Check for obstacles before moving or animating
  52.             if (Physics.Raycast(transform.position, transform.forward, scanDistance))
  53.             {
  54.                 // Obstacle detected, stop animation immediately
  55.                 animator.SetBool("isRunning", false);
  56.  
  57.                 // Decide what to do
  58.                 int decision = Random.Range(0, 2);
  59.                 if (decision == 0)
  60.                 {
  61.                     StopAndWait();
  62.                 }
  63.                 else
  64.                 {
  65.                     ChooseDirection();
  66.                 }
  67.             }
  68.             else
  69.             {
  70.                 // No obstacle, continue moving and animating
  71.                 animator.SetBool("isRunning", true);
  72.                 walkCounter -= Time.deltaTime;
  73.                 MoveInCurrentDirection();
  74.  
  75.                 if (walkCounter <= 0)
  76.                 {
  77.                     StopAndWait();
  78.                 }
  79.             }
  80.         }
  81.         else
  82.         {
  83.             waitCounter -= Time.deltaTime;
  84.  
  85.             if (waitCounter <= 0)
  86.             {
  87.                 ChooseDirection();
  88.             }
  89.         }
  90.     }
  91.  
  92.     void MoveInCurrentDirection()
  93.     {
  94.         switch (WalkDirection)
  95.         {
  96.             case 0:
  97.                 transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
  98.                 break;
  99.             case 1:
  100.                 transform.localRotation = Quaternion.Euler(0f, 90, 0f);
  101.                 break;
  102.             case 2:
  103.                 transform.localRotation = Quaternion.Euler(0f, -90, 0f);
  104.                 break;
  105.             case 3:
  106.                 transform.localRotation = Quaternion.Euler(0f, 180, 0f);
  107.                 break;
  108.         }
  109.         transform.position += transform.forward * moveSpeed * Time.deltaTime;
  110.     }
  111.  
  112.     void StopAndWait()
  113.     {
  114.         stopPosition = transform.position;
  115.         isWalking = false;
  116.         transform.position = stopPosition;
  117.         animator.SetBool("isRunning", false);
  118.         waitCounter = waitTime;
  119.     }
  120.  
  121.  
  122.     public void ChooseDirection()
  123.     {
  124.         int maxAttempts = 8;
  125.         int attempts = 0;
  126.         bool clearPath = false;
  127.  
  128.         while (!clearPath && attempts < maxAttempts)
  129.         {
  130.             float randomRotation = Random.Range(0, 360);
  131.             Quaternion rotation = Quaternion.Euler(0f, randomRotation, 0f);
  132.             Vector3 direction = rotation * Vector3.forward;
  133.  
  134.             if (!Physics.Raycast(transform.position, direction, scanDistance))
  135.             {
  136.                 clearPath = true;
  137.                 transform.rotation = rotation;
  138.             }
  139.             else
  140.             {
  141.                 attempts++;
  142.             }
  143.         }
  144.  
  145.         if (!clearPath)
  146.         {
  147.             WalkDirection = Random.Range(0, 4);
  148.         }
  149.         else
  150.         {
  151.             float angle = transform.rotation.eulerAngles.y;
  152.             if (angle >= 315 || angle < 45) WalkDirection = 0;
  153.             else if (angle >= 45 && angle < 135) WalkDirection = 1;
  154.             else if (angle >= 135 && angle < 225) WalkDirection = 3;
  155.             else WalkDirection = 2;
  156.         }
  157.  
  158.         isWalking = true;
  159.         walkCounter = walkTime;
  160.  
  161.         // Make sure the animation starts when a new direction is chosen
  162.         animator.SetBool("isRunning", true);
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement