Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

rts code tasking the civilian to harvest

Discussion in 'Scripting' started by PliscoVlad, Feb 27, 2020.

  1. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class ObjectInfo : MonoBehaviour {
    7.  
    8.     public NodeManager.ResourceTypes heldResourceType;
    9.     public bool isSelected = false;
    10.     public bool isGathering = false;
    11.  
    12.     public string objectName;
    13.  
    14.     private NavMeshAgent agent;
    15.  
    16.  
    17.     public int heldResource;
    18.     public int maxHeldResource;
    19.  
    20.     public GameObject[] drops;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         StartCoroutine(GatherTick());
    25.         agent = GetComponent<NavMeshAgent>();
    26.      
    27.     }
    28.    
    29.     // Update is called once per frame
    30.     void Update () {
    31.         if (heldResource >= maxHeldResource)
    32.         {
    33.             //Drop off point here
    34.             drops = GameObject.FindGameObjectsWithTag("Drops");
    35.             agent.destination = GetClosestDropOff(drops).transform.position;
    36.             drops = null;
    37.         }
    38.         if (Input.GetMouseButtonDown(1) && isSelected)
    39.         {
    40.             RightClick();
    41.         }
    42.     }
    43.  
    44.     GameObject GetClosestDropOff(GameObject[] dropOffs)
    45.     {
    46.         GameObject closestDrop = null;
    47.         float closestDistance = Mathf.Infinity;
    48.         Vector3 position = transform.position;
    49.  
    50.         foreach(GameObject targetDrop in dropOffs)
    51.         {
    52.             Vector3 direction = targetDrop.transform.position - position;
    53.             float distance = direction.sqrMagnitude;
    54.             if(distance < closestDistance)
    55.             {
    56.                 closestDistance = distance;
    57.                 closestDrop = targetDrop;
    58.             }
    59.         }
    60.  
    61.         return closestDrop;
    62.     }
    63.  
    64.  
    65.  
    66.  
    67.     public void RightClick()
    68.     {
    69.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    70.         RaycastHit hit;
    71.  
    72.         if(Physics.Raycast(ray, out hit, 100))
    73.         {
    74.             if (hit.collider.tag == "Ground")
    75.             {
    76.                 agent.destination = hit.point;
    77.                 Debug.Log("Moving");
    78.             }else if(hit.collider.tag == "Ressource")
    79.             {
    80.                 agent.destination = hit.collider.gameObject.transform.position;
    81.                 Debug.Log("Harvesting");
    82.             }
    83.         }
    84.     }
    85.  
    86.  
    87.     public void OnTriggerEnter(Collider other)
    88.     {
    89.         GameObject hitObject = other.gameObject;
    90.  
    91.         if(hitObject.tag == "Resource")
    92.         {
    93.             isGathering = true;
    94.             hitObject.GetComponent<NodeManager>().gatherers++;
    95.             heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
    96.         }
    97.     }
    98.  
    99.     public void OnTriggerExit(Collider other)
    100.     {
    101.         GameObject hitObject = other.gameObject;
    102.  
    103.         if(hitObject.tag == "Ressource")
    104.         {
    105.            
    106.             hitObject.GetComponent<NodeManager>().gatherers--;
    107.         }
    108.     }
    109.     IEnumerator GatherTick()
    110.     {
    111.         while (true)
    112.         {
    113.             yield return new WaitForSeconds(1);
    114.             if (isGathering)
    115.             {
    116.                 heldResource++;
    117.             }
    118.            
    119.         }
    120.     }
    121. }
     
  2. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    He doesn't collect the civilian and there is the stone to collect.What the solution this code?
     
  3. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    who her collect the stone
     
  4. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Is the tag "resource" or "ressource"? You have it spelled both ways in the script.
     
  5. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    yes but in which is line this script you say?
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    78, 91, 103
     
  7. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    in the tag, i put ressource and it don't put on error... normally i check this script and it ok... if you fond the problem or problems...say it to me , please?
     
  8. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    that's not what i meant to say sorrry
     
  9. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
  10. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    je meant if you have the solution please tell me
     
  11. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    i am sorry for you insult
     
  12. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    je suis desole pour mon anglais .. je parle que francais
     
  13. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    The compiler will not find the error in the naming because it is misspelled inside of a string variable. Thats one of the horrors of strings, its up to you to get the name right.
     
  14. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    thank you very much
     
  15. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    [code = CSharp] utilisant System.Collections;
    using System.Collections.Generic;
    en utilisant UnityEngine;
    using UnityEngine.AI;

    public class Civilian: MonoBehaviour {

    tâche TaskList publique;
    public RessourceManager RM;

    ActionList AL privée;

    GameObject targetNode;

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

    agent NavMeshAgent privé;

    public int holdResource;
    public int maxHeldResource;
    GameObject public [] tombe;

    // Utilisez ceci pour l'initialisation
    void Start () {
    StartCoroutine (GatherTick ());
    agent = GetComponent <NavMeshAgent> ();
    AL = FindObjectOfType <ActionList> ();
    }

    // La mise à jour est appelée une fois par image
    void Update () {
    if (targetNode == null)
    {
    if (holdResource! = 0)
    {
    drops = GameObject.FindGameObjectsWithTag ("Drops");
    agent.destination = GetClosestDropOff (drops) .transform.position;
    drops = null;
    task = TaskList.Delivering;
    }
    autre
    {
    task = TaskList.Idle;
    }
    }
    if (holdResource> = maxHeldResource)
    {
    // Point de dépose ici
    drops = GameObject.FindGameObjectsWithTag ("Drops");
    agent.destination = GetClosestDropOff (drops) .transform.position;
    drops = null;
    task = TaskList.Delivering;
    }
    if (Input.GetMouseButtonDown (1) && GetComponent <ObjectInfo> () .isSelected)
    {
    Clic-droit();
    }
    }

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

    foreach (GameObject targetDrop dans dropOffs)
    {
    Vector3 direction = targetDrop.transform.position - position;
    distance flottante = direction.sqrMagnitude;
    if (distance <plusDistanceDistance)
    {
    plusDistance = distance;
    étroitementDrop = targetDrop;
    }
    }

    renvoyer le plus procheDrop;
    }

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

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

    AL.Move (agent, hit, tâche);
    }
    sinon si (hit.collider.tag == "Ressource")
    {

    AL.Harvest (agent, hit, tâche, targetNode);
    }
    }
    }

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

    if (hitObject.tag == "Ressource" && task == TaskList.Gathering)
    {
    isGathering = true;
    hitObject.GetComponent <NodeManager> () .gatherers ++;
    holdResourceType = hitObject.GetComponent <NodeManager> () .resourceType;
    }
    sinon si (hitObject.tag == "Drops" && task == TaskList.Delivering)
    {
    if (RM.stone> = RM.maxStone)
    {
    task = TaskList.Idle;
    }
    autre
    {
    RM.stone + = holdResource;
    holdResource = 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++;
    }

    }
    }
    }
    [/code]
     
  16. PliscoVlad

    PliscoVlad

    Joined:
    Feb 14, 2020
    Posts:
    15
    je y a une erreur dans le targetnode dans ce code .... quelle est la solution...c est le code pour le civil