Page 1 sur 2

Erreur avec le targetnode

Publié : 15 Mars 2020 13:32
par Pompeignus
[DB-AL] Erreur avec le targetNode du civil a partir de ce code:

Code : Tout sélectionner

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

public class Civilian : MonoBehaviour {

    public TaskList task;
    public RessourceManager RM;

    private ActionList AL;

    GameObject targetNode;

    public NodeManager.ResourceTypes heldResourceType;
    public bool isGathering = false;

    private NavMeshAgent agent;

    public int heldResource;
    public int maxHeldResource;
    public GameObject[] drops;

    // Use this for initialization
    void Start () {
        StartCoroutine(GatherTick());
        agent = GetComponent<NavMeshAgent>();
        AL = FindObjectOfType<ActionList>();
    }
	
	// Update is called once per frame
	void Update () {
        if (targetNode == null)
        {
            if (heldResource != 0)
            {
                drops = GameObject.FindGameObjectsWithTag("Drops");
                agent.destination = GetClosestDropOff(drops).transform.position;
                drops = null;
                task = TaskList.Delivering;
            }
            else
            {
                task = TaskList.Idle;
            }
        }
        if (heldResource >= maxHeldResource)
        {
            //Drop off point here
            drops = GameObject.FindGameObjectsWithTag("Drops");
            agent.destination = GetClosestDropOff(drops).transform.position;
            drops = null;
            task = TaskList.Delivering;
        }
        if (Input.GetMouseButtonDown(1) && GetComponent<ObjectInfo>().isSelected)
        {
            RightClick();
        }
    }

    GameObject GetClosestDropOff(GameObject[] dropOffs)
    {
        GameObject closestDrop = null;
        float closestDistance = Mathf.Infinity;
        Vector3 position = transform.position;

        foreach (GameObject targetDrop in dropOffs)
        {
            Vector3 direction = targetDrop.transform.position - position;
            float distance = direction.sqrMagnitude;
            if (distance < closestDistance)
            {
                closestDistance = distance;
                closestDrop = targetDrop;
            }
        }

        return closestDrop;
    }

    public void RightClick()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            if (hit.collider.tag == "Ground")
            {

                AL.Move(agent, hit, task);
            }
            else if (hit.collider.tag == "Ressource")
            {

                AL.Harvest(agent, hit, task, targetNode);
            }
        }
    }

    public void OnTriggerEnter(Collider other)
    {
        GameObject hitObject = other.gameObject;

        if (hitObject.tag == "Ressource" && task == TaskList.Gathering)
        {
            isGathering = true;
            hitObject.GetComponent<NodeManager>().gatherers++;
            heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
        }
        else if (hitObject.tag == "Drops" && task == TaskList.Delivering)
        {
            if (RM.stone >= RM.maxStone)
            {
                task = TaskList.Idle;
            }
            else
            {
                RM.stone += heldResource;
                heldResource = 0;
                task = TaskList.Gathering;
                agent.destination = targetNode.transform.position;
            }
        }
    }


    public void OnTriggerExit(Collider other)
    {
        GameObject hitObject = other.gameObject;

        if (hitObject.tag == "Ressource")
        {

            hitObject.GetComponent<NodeManager>().gatherers--;
            isGathering = false;
        }
    }
    IEnumerator GatherTick()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            if (isGathering)
            {
                heldResource++;
            }

        }
    }
}
Je ne vois pas ou c est l erreur...quelle est la solution?

Re: Erreur avec le targetnode

Publié : 15 Mars 2020 13:58
par Aelhan
Bonjour (la bienséance veut que la plupart des posts commencent ainsi),

Peux tu nous dire quelle est l'erreur rencontrée (celle que renvoie la console) ? Ce sera bien plus facile pour tout le monde.

Re: Erreur avec le targetnode

Publié : 15 Mars 2020 14:38
par Pompeignus
Quand je sélection le civil,il ne ramasse pas les ressources et il ne s affiche pas l icone du civil lorsque je le sélection... il me marque ceci : Assets/Script/Civilian.cs(13,16): warning CS0649: Field `Civilian.targetNode' is never assigned to, and will always have its default value `null'

Re: Erreur avec le targetnode

Publié : 15 Mars 2020 15:09
par Max
Bonjour,

Tes explications sont vraiment vagues, et ne permettent pas vraiment de se faire une idée réelle de ton soucis.
D’où sort le code ? il y a pas mal de dépendance visiblement.
Sinon, ton soucis au niveau du targetNode est peut-être du au fait qu'il reste à null. As-tu bien des objets avec le tag "Drops" ?

Re: Erreur avec le targetnode

Publié : 15 Mars 2020 17:36
par Aelhan
`Civilian.targetNode' is never assigned to, and will always have its default value `null'

En effet, ton GameObject targetNode est privé n'est jamais assigné, donc forcément ça pose des problèmes à Unity, surtout que dans ton script tu essaies d'utiliser son transform à un moment.
Donc tu as 2 solutions, soit tu le passes en public et tu lui assignes un GameObject (via inspector ou un autre script) ou tu le laisses en privé, mais il faut que dans ton Start() tu lui assignes quelque chose (et éventuellement selon les conditions et tes besoins, lui assigner autre chose dynamiquement dans une autre fonction).

Re: Erreur avec le targetnode

Publié : 19 Mars 2020 11:58
par Pompeignus
[DB-AL]

Code : Tout sélectionner

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

public class Civilian : MonoBehaviour {

    public TaskList task;
    public RessourceManager RM;

    private ActionList AL;

    public GameObject targetNode;

    public NodeManager.ResourceTypes heldResourceType;
    public bool isGathering = false;

    private NavMeshAgent agent;

    public int heldResource;
    public int maxHeldResource;
    public GameObject[] drops;

    public float distToTarget;

    public bool isGatherer = false;

    // Use this for initialization
    void Start () {
        StartCoroutine(GatherTick());
        agent = GetComponent<NavMeshAgent>();
        AL = FindObjectOfType<ActionList>();
    }
	
	// Update is called once per frame
	void Update () {


        if (task == TaskList.Gathering)
        {
            distToTarget = Vector3.Distance(transform.position, targetNode.transform.position);
            if (distToTarget <= 3.5f)
            {
                Gather();
            }
        }

        if(task == TaskList.Delivering)
        {
            if(distToTarget <= 3.5f)
            {
                if(RM.stone >= RM.maxStone)
                {
                    task = TaskList.Idle;
                }
                else
                {
                    RM.stone += heldResource;
                    heldResource = 0;
                    task = TaskList.Gathering;
                    agent.destination = targetNode.transform.position;
                    isGatherer = false;
                }
            }
        }

        if (targetNode == null)
        {
            if (heldResource != 0)
            {
                drops = GameObject.FindGameObjectsWithTag("Drops");
                agent.destination = GetClosestDropOff(drops).transform.position;
                distToTarget = Vector3.Distance(GetClosestDropOff(drops).transform.position, transform.position);
                drops = null;
                task = TaskList.Delivering;
            }
            else
            {
                task = TaskList.Idle;
            }
        }
        if (heldResource >= maxHeldResource)
        {
            //Drop off point here
            targetNode.GetComponent<NodeManager>().gatherers--;
            isGathering = false;
            drops = GameObject.FindGameObjectsWithTag("Drops");
            agent.destination = GetClosestDropOff(drops).transform.position;
            distToTarget = Vector3.Distance(GetClosestDropOff(drops).transform.position, transform.position);
            drops = null;
            task = TaskList.Delivering;
            GetComponent<NavMeshObstacle>().enabled = false;
            GetComponent<NavMeshAgent>().enabled = true;
        }
        if (Input.GetMouseButtonDown(1) && GetComponent<ObjectInfo>().isSelected)
        {
            RightClick();
        }
    }

    GameObject GetClosestDropOff(GameObject[] dropOffs)
    {
        GameObject closestDrop = null;
        float closestDistance = Mathf.Infinity;
        Vector3 position = transform.position;

        foreach (GameObject targetDrop in dropOffs)
        {
            Vector3 direction = targetDrop.transform.position - position;
            float distance = direction.sqrMagnitude;
            if (distance < closestDistance)
            {
                closestDistance = distance;
                closestDrop = targetDrop;
            }
        }

        return closestDrop;
    }

    public void RightClick()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            if (hit.collider.tag == "Ground")
            {
                targetNode.GetComponent<NodeManager>().gatherers--;
                isGathering = false;
                AL.Move(agent, hit, task);
                task = TaskList.Moving;
                GetComponent<NavMeshObstacle>().enabled = false;
                GetComponent<NavMeshAgent>().enabled = true;
            }
            else if (hit.collider.tag == "Ressource")
            {

                AL.Move(agent, hit, task);
                targetNode = hit.collider.gameObject;
                task = TaskList.Gathering;
            }

            else if (hit.collider.tag == "Drops")
            {
                targetNode.GetComponent<NodeManager>().gatherers--;
                isGathering = false;
                drops = GameObject.FindGameObjectsWithTag("Drops");
                agent.destination = GetClosestDropOff(drops).transform.position;
                distToTarget = Vector3.Distance(GetClosestDropOff(drops).transform.position, transform.position);
                drops = null;
                task = TaskList.Delivering;
                GetComponent<NavMeshObstacle>().enabled = false;
                GetComponent<NavMeshAgent>().enabled = true;
            }
        }
    }

    public void Gather()
    {
        isGathering = true;
        if (!isGatherer)
        {
            targetNode.GetComponent<NodeManager>().gatherers++;
            isGatherer = true;
        }
        heldResourceType = targetNode.GetComponent<NodeManager>().resourceType;
        GetComponent<NavMeshObstacle>().enabled = true;
        GetComponent<NavMeshAgent>().enabled = false;
        
    }

    //public void OnTriggerEnter(Collider other)
    //{
    //    GameObject hitObject = other.gameObject;

    //    if (hitObject.tag == "Ressource" && task == TaskList.Gathering)
    //    {
    //        isGathering = true;
    //        hitObject.GetComponent<NodeManager>().gatherers++;
    //        heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
    //        GetComponent<NavMeshObstacle>().enabled = true;
    //        GetComponent<NavMeshAgent>().enabled = false;
    //    }
    //    else if (hitObject.tag == "Drops" && task == TaskList.Delivering)
    //    {
    //        if (RM.stone >= RM.maxStone)
    //        {
    //            task = TaskList.Idle;
    //        }
    //        else
    //        {
    //            RM.stone += heldResource;
    //            heldResource = 0;
    //            task = TaskList.Gathering;
    //            agent.destination = targetNode.transform.position;
    //        }
    //    }
    //}


    IEnumerator GatherTick()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            if (isGathering)
            {
                heldResource++;
            }

        }
    }
}

J ai deux problème;
1) il me dit ceci : "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:set_destination(Vector3)
Civilian:Update() (at Assets/Script/Civilian.cs:88)
2) l autre me dit ceci : NavMeshAgent and NavMeshObstacle components are active at the same time. This can lead to errorneous behavior.

Je ne sais pas ou c est l erreur...Est-ce que vous avez une ou des idée(s)?Si oui, lequelles?

Re: Erreur avec le targetnode

Publié : 19 Mars 2020 12:28
par Max
Bonjour,

question bête: as-tu bien un NavMeshAgent (et actif) sur ton GameObject ?

Re: Erreur avec le targetnode

Publié : 19 Mars 2020 12:56
par Pompeignus
oui

Re: Erreur avec le targetnode

Publié : 19 Mars 2020 14:20
par Max
A mon avis tu cherches à donner une destination à ton NavMeshAgent et en même temps tu dois jouer sur le tranform par un repositionnement du GameObject.

Re: Erreur avec le targetnode

Publié : 19 Mars 2020 15:34
par Pompeignus
Je ne sais pas mais j ai un autre problème , est-ce que vous trouvez bizarre que le civil se déplace tout seul?(sans animation) Quelle est la solution?