Untitled
unknown
plain_text
a year ago
10 kB
7
Indexable
using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.AI; using UnityEngine.Networking; using UnityEngine.Rendering; public class CustomerAI : MonoBehaviour { //public float moveSpeed = 2.0f; //public Animator animator; //private Transform[] waypoints; // Changed from public to private //private int currentWaypointIndex = 0; //void Start() //{ // animator = GetComponent<Animator>(); // if (animator == null) // { // Debug.LogWarning("Animator component not found!"); // } // // Find waypoints with the tag "WayPointObject" // GameObject[] waypointObjects = GameObject.FindGameObjectsWithTag("WayPointObject"); // waypoints = new Transform[waypointObjects.Length]; // for (int i = 0; i < waypointObjects.Length; i++) // { // waypoints[i] = waypointObjects[i].transform; // } // if (waypoints.Length > 0) // { // // Start moving towards the first waypoint // MoveTowards(waypoints[currentWaypointIndex]); // } // else // { // Debug.LogError("No waypoints found with tag 'WayPointObject'!"); // } //} //void Update() //{ // if (currentWaypointIndex < waypoints.Length) // { // MoveTowards(waypoints[currentWaypointIndex]); // } //} //void MoveTowards(Transform target) //{ // // Calculate direction in the XZ plane // Vector3 targetPosition = new Vector3(target.position.x, transform.position.y, target.position.z); // Vector3 direction = (targetPosition - transform.position).normalized; // // Rotate towards the target // transform.rotation = Quaternion.LookRotation(direction); // // Calculate movement in the XZ plane // Vector3 newPosition = transform.position + direction * moveSpeed * Time.deltaTime; // transform.position = new Vector3(newPosition.x, transform.position.y, newPosition.z); // // Check if reached the waypoint // if (Vector3.Distance(transform.position, targetPosition) < 0.1f) // { // Debug.Log("Reached waypoint: " + targetPosition); // MoveToNextWaypoint(); // } //} //void MoveToNextWaypoint() //{ // currentWaypointIndex++; // if (currentWaypointIndex < waypoints.Length) // { // MoveTowards(waypoints[currentWaypointIndex]); // } //} //private void OnCollisionEnter(Collision collision) //{ // if (collision.gameObject.CompareTag("HallCupboard")) // { // animator.SetBool("CustomerIdle", true); // } //} public float moveSpeed = 2.0f; public Animator animator; private List<Transform> waypoints = new List<Transform>(); private int currentWaypointIndex = 0; public bool CustomerhasShoe = false; public int platformNumber = 0; // This will determine which platform the customer should go to public Transform customerHand; public CustomerData customerData; // Reference to the CustomerData asset void Start() { animator = GetComponent<Animator>(); if (animator == null) { Debug.LogWarning("Animator component not found!"); } // Find "CustomerWayPoints" GameObject GameObject customerWayPoints = GameObject.Find("CustomerWayPoints"); if (customerWayPoints != null) { // Get all child transforms (waypoints) of "CustomerWayPoints" foreach (Transform child in customerWayPoints.transform) { waypoints.Add(child); } if (waypoints.Count > 0) { MoveTowards(waypoints[currentWaypointIndex]); } else { Debug.LogError("No waypoints found under 'CustomerWayPoints'!"); } } else { Debug.LogError("GameObject 'CustomerWayPoints' not found!"); } } void Update() { if (currentWaypointIndex < waypoints.Count) { MoveTowards(waypoints[currentWaypointIndex]); } } void MoveTowards(Transform target) { Vector3 targetPosition = new Vector3(target.position.x, transform.position.y, target.position.z); Vector3 direction = (targetPosition - transform.position).normalized; transform.rotation = Quaternion.LookRotation(direction); Vector3 newPosition = transform.position + direction * moveSpeed * Time.deltaTime; transform.position = new Vector3(newPosition.x, transform.position.y, newPosition.z); if (Vector3.Distance(transform.position, targetPosition) < 0.1f) { Debug.Log("Reached waypoint: " + targetPosition); MoveToNextWaypoint(); } else { // Check if there are shoes available at the current waypoint CheckShoesAtWaypoint(); } } void MoveToNextWaypoint() { currentWaypointIndex++; if (currentWaypointIndex < waypoints.Count) { MoveTowards(waypoints[currentWaypointIndex]); } else { // After reaching all waypoints, go to the assigned platform GoToPlatform(platformNumber); } } void CheckShoesAtWaypoint() { if (!CustomerhasShoe && HallCupboardManager.instance != null && HallCupboardManager.instance.placedShoes.Count > 0) { Transform currentWaypoint = waypoints[currentWaypointIndex]; foreach (Transform shoe in HallCupboardManager.instance.placedShoes) { if (Vector3.Distance(shoe.position, currentWaypoint.position) < 1.0f) { HallCupboardManager.instance.RemoveShoe(shoe); CustomerhasShoe = true; animator.SetBool("CustomerIdle", true); Debug.Log("Customer took a shoe at waypoint: " + currentWaypoint.position); break; } else { animator.SetBool("CustomerIdle", false); } } } } void GoToPlatform(int platformIndex) { switch (platformIndex) { case 1: // Move to platform 1 Debug.Log("Moving to platform 1"); break; case 2: // Move to platform 2 Debug.Log("Moving to platform 2"); break; default: Debug.LogWarning("Platform index out of range"); break; } } private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("HallCupboard")) { CheckCupboard(); StartCoroutine(CheckCupboardPeriodically()); } } public float checkInterval = 5f; IEnumerator CheckCupboardPeriodically() { yield return new WaitForSeconds(15f); // Wait 5 seconds before the first periodic check while (true) { CheckCupboard(); yield return new WaitForSeconds(15f); // Wait 5 seconds before the next check } } void CheckCupboard() { if (HallCupboardManager.instance != null) { bool isEmpty = HallCupboardManager.instance.IsCupboardEmpty(); Debug.Log("Is cupboard empty: " + isEmpty); if (isEmpty) { Debug.Log("Hall cupboard is empty. Customer is idle."); animator.SetBool("CustomerIdle", true); } else { Debug.Log("Hall cupboard is not empty. Customer can buy item."); animator.SetBool("CustomerIdle", false); TakeShoe(); } } else { Debug.LogWarning("HallCupboardManager.instance reference not set!"); } } //void TakeShoe() //{ // if (!CustomerhasShoe && HallCupboardManager.instance.placedShoes.Count > 0) // { // Transform shoe = HallCupboardManager.instance.placedShoes[0]; // HallCupboardManager.instance.RemoveShoe(shoe); // CustomerhasShoe = true; // animator.SetBool("CarryWalk", true); // animator.SetBool("WakingCustomer", false); // animator.SetBool("CustomerIdle", false); // Debug.Log("Customer took a shoe."); // if (waypoints.Count > 0) // { // waypoints.RemoveAt(0); // currentWaypointIndex = 0; // if (waypoints.Count > 0) // { // MoveTowards(waypoints[currentWaypointIndex]); // } // } // } //} void TakeShoe() { if (!CustomerhasShoe && HallCupboardManager.instance.placedShoes.Count > 0) { Transform shoe = HallCupboardManager.instance.placedShoes[0]; HallCupboardManager.instance.RemoveShoe(shoe); CustomerhasShoe = true; animator.SetBool("CarryWalk", true); Debug.Log("Customer took a shoe."); AttachShoeToHand(shoe); if (waypoints.Count > 0) { waypoints.RemoveAt(0); currentWaypointIndex = 0; if (waypoints.Count > 0) { MoveTowards(waypoints[currentWaypointIndex]); } } } } void AttachShoeToHand(Transform shoe) { shoe.SetParent(customerHand); shoe.localPosition = Vector3.zero; // Adjust the position relative to the hand if needed shoe.localRotation = Quaternion.identity; // Adjust the rotation if needed } }
Editor is loading...
Leave a Comment