Search Unity

Instantiated NPC will not change from order taken state to movement state, when original NPC will.

Discussion in 'Game Design' started by starpandy, Jan 13, 2023.

  1. starpandy

    starpandy

    Joined:
    Aug 25, 2021
    Posts:
    3
    Iv been struggling with this for a while and tried so many different solutions. Im creating a bar management game and iv got an enum state machine which works perfectly for my original NPC. but when the create npc function executes it creates the npc, it is in the idle state and when i click it the state changes to order taken, works great until i set order complete to true using the same way as my original npc through my serve mat, but it just wont change to the movement state which it should.
    what am i doing wrong?


    NPCAI
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;

    public class NPCAI : MonoBehaviour
    {

    public Transform movePosTransformBar;
    public Transform movePosTransformChair;
    public Transform movePosTransformChair2;
    public Transform movePosTransformChair3;
    public Transform movePosTransformChair4;

    public bool satIn1 = false;
    public bool satIn2 = false;
    public bool satIn3 = false;
    public bool satIn4 = false;

    public Animator animator;

    public float randomNumber;

    public NavMeshAgent NavMeshAgent;

    public bool orderComplete = false;

    public GameObject order;

    public int orderValue = 1;

    public Transform npcRotate;

    public GameObject NPC;

    bool hasRun = false;
    public enum CustomerState
    {
    IDLE,
    ORDER_TAKEN,
    MOVEMENT,
    SITTING
    }

    public CustomerState currentState;
    public bool isSelected; // Flag to check if the customer is selected

    public static List<NPCAI> allCustomers = new List<NPCAI>();

    void Start()
    {
    allCustomers.Add(this);

    currentState = CustomerState.IDLE;

    isSelected = false;

    npcRotate = GetComponent<Transform>();

    NavMeshAgent = GetComponent<NavMeshAgent>();

    animator = GetComponent<Animator>();

    randomNumber = Random.Range(1, 5);

    if (satIn1 == true && randomNumber == 1)
    {
    randomNumber = Random.Range(1, 5);
    }

    if (satIn2 == true && randomNumber == 2)
    {
    randomNumber = Random.Range(1, 5);
    }

    if (satIn3 == true && randomNumber == 3)
    {
    randomNumber = Random.Range(1, 5);
    }

    if (satIn4 == true && randomNumber == 4)
    {
    randomNumber = Random.Range(1, 5);
    }

    Debug.Log(randomNumber);

    }

    void Update()
    {
    switch (currentState)
    {
    case CustomerState.IDLE:


    NavMeshAgent.destination = movePosTransformBar.position;
    // Idle state code here
    if (isSelected && order.activeSelf == true)
    {
    currentState = CustomerState.ORDER_TAKEN;
    }
    break;
    case CustomerState.ORDER_TAKEN:

    SelectCustomer();
    order.SetActive(true);
    if (orderComplete == true)
    {
    currentState = CustomerState.MOVEMENT;
    }


    break;
    case CustomerState.MOVEMENT:
    orderComplete = false;
    DeselectCustomer();
    order.SetActive(false);
    animator.Play("pick up");

    if(hasRun == false)
    {
    hasRun = true;
    createNewNPC();

    }
    hasRun = true;


    if (animator.velocity.magnitude == 0)
    {
    currentState = CustomerState.SITTING;
    }
    break;
    case CustomerState.SITTING:
    hasRun = false;
    // Sitting state code here
    break;
    default:
    break;
    }


    }

    public void SelectCustomer()
    {
    order.SetActive(true);
    isSelected = true;
    }

    public void DeselectCustomer()
    {
    order.SetActive(false);
    isSelected = false;
    }
    void OnMouseDown()
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
    if (hit.transform.CompareTag("NPC"))
    {
    DeselectAllCustomers();
    SelectCustomer();
    }
    }
    }

    public void DeselectAllCustomers()
    {
    foreach (NPCAI NPC in allCustomers)
    {
    NPC.DeselectCustomer();
    }
    }
    public void rotateNPC()
    {
    if (satIn1 == true)
    {
    npcRotate.transform.Rotate(0.0f, 70.0f, 0.0f);
    }

    if (satIn2 == true)
    {
    npcRotate.transform.Rotate(0.0f, 100.0f, 0.0f);
    }

    if (satIn3 == true)
    {
    npcRotate.transform.Rotate(0.0f, 100.0f, 0.0f);
    }

    if (satIn4 == true)
    {
    npcRotate.transform.Rotate(0.0f, 100.0f, 0.0f);
    }
    }

    public void createNewNPC()
    {
    Instantiate(NPC);
    }

    public void moveCustomer()
    {;
    if (randomNumber == 1 && satIn1 == false)
    {

    satIn1 = true;
    rotateNPC();
    animator.Play("walk hold");
    NavMeshAgent.destination = movePosTransformChair.position;
    orderComplete = false;
    }

    if (randomNumber == 2 && satIn2 == false)
    {

    satIn2 = true;
    rotateNPC();
    animator.Play("walk hold");
    NavMeshAgent.destination = movePosTransformChair2.position;
    orderComplete = false;
    }

    if (randomNumber == 3 && satIn3 == false)
    {

    satIn3 = true;
    rotateNPC();
    animator.Play("walk hold");
    NavMeshAgent.destination = movePosTransformChair3.position;
    orderComplete = false;
    }

    if (randomNumber == 4 && satIn4 == false)
    {
    satIn4 = true;
    rotateNPC();
    animator.Play("walk hold");
    NavMeshAgent.destination = movePosTransformChair4.position;
    orderComplete = false;

    }
    }

    }

    ServeMat
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;

    public class ServeMat : MonoBehaviour
    {
    private bool itemOnMat = false;

    public Grabbable grab;

    public NPCAI ai;

    Animator animator;

    public int matValue = 0;
    private void Start()
    {

    GameObject temp = GameObject.FindGameObjectWithTag("NPC");
    ai = temp.GetComponent<NPCAI>();
    animator = temp.GetComponent<Animator>();

    GameObject tempB = GameObject.FindGameObjectWithTag("Grab");
    grab = tempB.GetComponent<Grabbable>();
    }

    private void OnCollisionEnter(Collision collision)
    {
    if (collision.gameObject.tag == "Grab")
    {
    matValue += grab.foodValue;
    Debug.Log(matValue + " " + ai.orderValue);
    Debug.Log("collison detected");
    itemOnMat = true;
    }


    }

    private void OnCollisionExit(Collision collision)
    {
    matValue = 0;
    itemOnMat = false;
    Debug.Log(matValue + " " + ai.orderValue);
    }

    private void OnMouseDown()
    {
    OrderComplete();
    }

    public void OrderComplete()
    {
    Debug.Log("clicked");
    Debug.Log(ai.orderComplete);

    if (ai.orderValue == matValue && itemOnMat == true)
    {
    ai.orderComplete = true;
    Debug.Log("correct");
    matValue = 0;
    Debug.Log(ai.orderComplete);

    }
    }
    }