evelynshilosky

AI_Movement - Part 3

Jun 2nd, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 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.     float waitTime;
  18.     public float waitCounter;
  19.  
  20.     int WalkDirection;
  21.  
  22.     public bool isWalking;
  23.  
  24.     // Start is called before the first frame update
  25.     void Start()
  26.     {
  27.         animator = GetComponent<Animator>();
  28.  
  29.         //So that all the prefabs don't move/stop at the same time
  30.         walkTime = Random.Range(3,6);
  31.         waitTime = Random.Range(5,7);
  32.  
  33.  
  34.         waitCounter = waitTime;
  35.         walkCounter = walkTime;
  36.  
  37.         ChooseDirection();
  38.     }
  39.  
  40.     // Update is called once per frame
  41.     void Update()
  42.     {
  43.         if (isWalking)
  44.         {
  45.  
  46.             animator.SetBool("isRunning", true);
  47.  
  48.             walkCounter -= Time.deltaTime;
  49.  
  50.             switch (WalkDirection)
  51.             {
  52.                 case  0:
  53.                     transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
  54.                     transform.position += transform.forward * moveSpeed * Time.deltaTime;
  55.                     break;
  56.                 case  1:
  57.                     transform.localRotation = Quaternion.Euler(0f, 90, 0f);
  58.                     transform.position += transform.forward * moveSpeed * Time.deltaTime;
  59.                     break;
  60.                 case  2:
  61.                     transform.localRotation = Quaternion.Euler(0f, -90, 0f);
  62.                     transform.position += transform.forward * moveSpeed * Time.deltaTime;
  63.                     break;
  64.                 case  3:
  65.                     transform.localRotation = Quaternion.Euler(0f, 180, 0f);
  66.                     transform.position += transform.forward * moveSpeed * Time.deltaTime;
  67.                     break;
  68.             }
  69.  
  70.             if (walkCounter <= 0)
  71.             {
  72.                 stopPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
  73.                 isWalking = false;
  74.                 //stop movement
  75.                 transform.position = stopPosition;
  76.                 animator.SetBool("isRunning", false);
  77.                 //reset the waitCounter
  78.                 waitCounter = waitTime;
  79.             }
  80.  
  81.  
  82.         }
  83.         else
  84.         {
  85.  
  86.             waitCounter -= Time.deltaTime;
  87.  
  88.             if (waitCounter <= 0)
  89.             {
  90.                 ChooseDirection();
  91.             }
  92.         }
  93.     }
  94.  
  95.  
  96.     public void ChooseDirection()
  97.     {
  98.         WalkDirection = Random.Range(0, 4);
  99.  
  100.         isWalking = true;
  101.         walkCounter = walkTime;
  102.     }
  103. }
Add Comment
Please, Sign In to add comment