Search Unity

NPCState

Discussion in 'Navigation' started by Zoro321, Jan 6, 2021.

  1. Zoro321

    Zoro321

    Joined:
    Nov 15, 2013
    Posts:
    13
    Hello,


    Ok so I have been assigned to work on a Unity project developped by a third party with which communication is not possible and I need some help to understand their code.

    I have an character which walks through some "Waypoints". The Waypoints are defined by some empty objects having a transform.


    I have been asked to change the trajectory of the caracter by adding some new waypoints.

    For each Waypoint, the charatcter as a script named "MaillGoToPoisition" as shown here at the right in the inspector:

    upload_2021-1-6_15-53-16.png



    Already there there is something I so not understand. Why does each scipt has a "script name" variable ?


    Here is the script:

    using UnityEngine;
    public class MallGoToPosition : GoToPosition {
    public override void Do()
    {
    animator.SetBool("isWalking", true);
    Vector3 vec = agent.pathEndPosition - transform.position;
    vec.y = 0;
    if(vec.sqrMagnitude < 0.01f)
    {
    SendMessage("NextState");
    }
    }
    }


    Also in the inspector, my character has another script named "NPcStatesContoller", which contains an array of the "NPCState" data type. The problem is I can't understand how to set the elements of this NPCState array. They appear in green in the inspector and I do not understand what I should drag on it. They seem to be related to the "script name" variable defined for each GoToPosition script.


    upload_2021-1-6_15-58-0.png


    Here is the NPCStatesController script:

    using UnityEngine;
    public class NPCStatesController : NamedMonoBehaviour, IActivatable {

    public NPCState[] npcStates;
    public bool loops;

    public bool debug;
    public int startingStateIndex = 0;

    private int currentStateIndex = 0;

    void Awake()
    {
    currentStateIndex = startingStateIndex;
    }

    public void Activate ()
    {
    NextState();
    }
    public void Start()
    {
    if(npcStates.Length > currentStateIndex)
    npcStates[currentStateIndex].Enter();
    }
    public void Update()
    {
    if(debug)
    Debug.Log("Current State : " + npcStates[currentStateIndex].name);

    if(npcStates.Length > currentStateIndex)
    npcStates[currentStateIndex].Do();
    }

    public void NextState()
    {
    npcStates[currentStateIndex].Exit();
    ++currentStateIndex;

    if(loops && currentStateIndex == npcStates.Length)
    currentStateIndex = 0;

    npcStates[currentStateIndex].Enter();
    }
    }


    I searched everywhere and can not find the definition of "NPCState".

    It seems I am missing a part of the puzzle. There is something I do not grasp.

    Thanks alot !

     
  2. Zoro321

    Zoro321

    Joined:
    Nov 15, 2013
    Posts:
    13
    My message might be too long and unclear.


    I just want to know what I should drag in the nspector to fill the NPCState array. Those appear in green in the last picture such as [GoToWaipoint 1].


    I do not understand how to associate those NPCState s with my Waypoint transforms, which have matching script names associated.

    I understand it is a very specific question tightly linked to a specific implementation.
     
  3. Zoro321

    Zoro321

    Joined:
    Nov 15, 2013
    Posts:
    13
    Ok so I found the definition of NPCState:



    using UnityEngine;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    public abstract class NPCState : NamedMonoBehaviour {
    public abstract void Enter();
    public abstract void Do();
    public abstract void Exit();
    }



    And I realised that GoToPOsition inherits NPCState:


    using UnityEngine;
    public class GoToPosition : NPCState {

    public Transform target;

    protected UnityEngine.AI.NavMeshAgent agent;
    protected Animator animator;

    void Awake()
    {
    agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    agent.autoRepath = false;
    agent.autoBraking = true;
    agent.autoTraverseOffMeshLink = true;
    animator = GetComponent<Animator>();
    }

    public override void Enter()
    {
    agent.SetDestination(target.position);
    animator.SetBool("isWalking", true);
    }
    public override void Do()
    {
    Vector3 distance = agent.pathEndPosition - transform.position;
    distance.y = 0;
    if(agent.hasPath && distance.sqrMagnitude < 0.01f)
    {
    SendMessage("NextState");
    }
    }

    public override void Exit()
    {
    }

    void OnDrawGizmosSelected()
    {
    if(target != null && scriptName != "GoTo" + target.name)
    scriptName = "GoTo" + target.name;
    }
    }



    So, my problem is about "NamedMonoScriptBehaviour".. it has a script name field.


    That is where I am blocked around.

    I give a name to each MallGoToPosition script a name that must be refered into the PCState array. The problem is that I do not understand how reference a given script name into the NPCState array of the NPCStateController.