Advertisement
evelynshilosky

AI_Movement - Part 31

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