Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class AI_Movement : MonoBehaviour
- {
- Animator animator;
- public float moveSpeed = 0.2f;
- Vector3 stopPosition;
- float walkTime;
- public float walkCounter;
- public int walkTimeMin = 3;
- public int walkTimeMax = 6;
- float waitTime;
- public float waitCounter;
- int WalkDirection;
- public bool isWalking;
- // Object Detection Variables (Hopefully this will work)
- public float scanDistance = 5f;
- public float rotationIncrement = 45f; // Degree increment for rotation checks
- // Start is called before the first frame update
- void Start()
- {
- animator = GetComponent<Animator>();
- //So that all the prefabs don't move/stop at the same time
- walkTime = Random.Range(walkTimeMin, walkTimeMax);
- waitTime = Random.Range(5, 7);
- waitCounter = waitTime;
- walkCounter = walkTime;
- ChooseDirection();
- }
- // Update is called once per frame
- void Update()
- {
- if (isWalking)
- {
- // Check for obstacles before moving or animating
- if (Physics.Raycast(transform.position, transform.forward, scanDistance))
- {
- // Obstacle detected, stop animation immediately
- animator.SetBool("isRunning", false);
- // Decide what to do
- int decision = Random.Range(0, 2);
- if (decision == 0)
- {
- StopAndWait();
- }
- else
- {
- ChooseDirection();
- }
- }
- else
- {
- // No obstacle, continue moving and animating
- animator.SetBool("isRunning", true);
- walkCounter -= Time.deltaTime;
- MoveInCurrentDirection();
- if (walkCounter <= 0)
- {
- StopAndWait();
- }
- }
- }
- else
- {
- waitCounter -= Time.deltaTime;
- if (waitCounter <= 0)
- {
- ChooseDirection();
- }
- }
- }
- void MoveInCurrentDirection()
- {
- switch (WalkDirection)
- {
- case 0:
- transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
- break;
- case 1:
- transform.localRotation = Quaternion.Euler(0f, 90, 0f);
- break;
- case 2:
- transform.localRotation = Quaternion.Euler(0f, -90, 0f);
- break;
- case 3:
- transform.localRotation = Quaternion.Euler(0f, 180, 0f);
- break;
- }
- transform.position += transform.forward * moveSpeed * Time.deltaTime;
- }
- void StopAndWait()
- {
- stopPosition = transform.position;
- isWalking = false;
- transform.position = stopPosition;
- animator.SetBool("isRunning", false);
- waitCounter = waitTime;
- }
- public void ChooseDirection()
- {
- int maxAttempts = 8;
- int attempts = 0;
- bool clearPath = false;
- while (!clearPath && attempts < maxAttempts)
- {
- float randomRotation = Random.Range(0, 360);
- Quaternion rotation = Quaternion.Euler(0f, randomRotation, 0f);
- Vector3 direction = rotation * Vector3.forward;
- if (!Physics.Raycast(transform.position, direction, scanDistance))
- {
- clearPath = true;
- transform.rotation = rotation;
- }
- else
- {
- attempts++;
- }
- }
- if (!clearPath)
- {
- WalkDirection = Random.Range(0, 4);
- }
- else
- {
- float angle = transform.rotation.eulerAngles.y;
- if (angle >= 315 || angle < 45) WalkDirection = 0;
- else if (angle >= 45 && angle < 135) WalkDirection = 1;
- else if (angle >= 135 && angle < 225) WalkDirection = 3;
- else WalkDirection = 2;
- }
- isWalking = true;
- walkCounter = walkTime;
- // Make sure the animation starts when a new direction is chosen
- animator.SetBool("isRunning", true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement