Search Unity

Game Object not properly setting

Discussion in 'Scripting' started by jacob_p01, May 11, 2019.

  1. jacob_p01

    jacob_p01

    Joined:
    Nov 20, 2018
    Posts:
    8
    So I'm working on an RTS game, currently working on the AI for resource gathering. I'm trying to set the active resource node on the player's right click. For some reason, it isn't setting. Here is the rightClick method, where I'm attempting to set the active node.
    Code (CSharp):
    1. public void RightClick()
    2.     {
    3.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.         RaycastHit hit;
    5.  
    6.         if(Physics.Raycast(ray, out hit, 100))
    7.         {
    8.             if(hit.collider.tag == "Ground")
    9.             {
    10.                 agent.destination = hit.point;
    11.                 Debug.Log("Moving");
    12.                 task = TaskList.Moving;
    13.             }
    14.             else if(hit.collider.tag == "Resource")
    15.             {
    16.                 agent.destination = hit.collider.gameObject.transform.position;
    17.                 Debug.Log("Harvesting");
    18.                 task = TaskList.Gathering;
    19.                 targetNode = hit.collider.gameObject;
    20.             }
    21.         }
    22.     }
    If you need more insight into what is happening in the script, here is the full script:
    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 TaskList task;
    9.     public ResourceManager RM;
    10.  
    11.     public GameObject targetNode;
    12.  
    13.     public NodeManager.ResourceTypes heldResourceType;
    14.  
    15.     public bool isSelected = false;
    16.     public bool isGathering = false;
    17.  
    18.     public string objectName;
    19.  
    20.     private NavMeshAgent agent;
    21.  
    22.     public int heldResource;
    23.     public int maxHeldResource;
    24.  
    25.     GameObject[] drops;
    26.  
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.         StartCoroutine(GatherTick());
    31.         agent = GetComponent<NavMeshAgent>();
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         if(targetNode = null)
    38.         {
    39.             if(heldResource != 0)
    40.             {
    41.                 drops = GameObject.FindGameObjectsWithTag("Drops");
    42.                 agent.destination = GetClosestDropOff(drops).transform.position;
    43.                 drops = null;
    44.                 task = TaskList.Delivering;
    45.             }
    46.             else
    47.             {
    48.                 task = TaskList.Idle;
    49.             }
    50.         }
    51.  
    52.         if(heldResource >= maxHeldResource)
    53.         {
    54.             //Drop off point here
    55.             drops = GameObject.FindGameObjectsWithTag("Drops");
    56.             agent.destination = GetClosestDropOff(drops).transform.position;
    57.             drops = null;
    58.             task = TaskList.Delivering;
    59.         }
    60.  
    61.         if (Input.GetMouseButtonDown(1) && isSelected)
    62.         {
    63.             RightClick();
    64.         }
    65.  
    66.     }
    67.  
    68.     GameObject GetClosestDropOff(GameObject[] dropOffs)
    69.     {
    70.         GameObject closestDrop = null;
    71.         float closestDistance = Mathf.Infinity;
    72.         Vector3 position = transform.position;
    73.  
    74.         foreach(GameObject targetDrop in dropOffs)
    75.         {
    76.             Vector3 direction = targetDrop.transform.position - position;
    77.             float distance = direction.sqrMagnitude;
    78.  
    79.             if(distance < closestDistance)
    80.             {
    81.                 closestDistance = distance;
    82.                 closestDrop = targetDrop;
    83.             }
    84.         }
    85.  
    86.         return closestDrop;
    87.  
    88.     }
    89.  
    90.     public void RightClick()
    91.     {
    92.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    93.         RaycastHit hit;
    94.  
    95.         if(Physics.Raycast(ray, out hit, 100))
    96.         {
    97.             if(hit.collider.tag == "Ground")
    98.             {
    99.                 agent.destination = hit.point;
    100.                 Debug.Log("Moving");
    101.                 task = TaskList.Moving;
    102.             }
    103.             else if(hit.collider.tag == "Resource")
    104.             {
    105.                 agent.destination = hit.collider.gameObject.transform.position;
    106.                 Debug.Log("Harvesting");
    107.                 task = TaskList.Gathering;
    108.                 targetNode = hit.collider.gameObject;
    109.             }
    110.         }
    111.     }
    112.  
    113.     public void OnTriggerEnter(Collider other)
    114.     {
    115.         GameObject hitObject = other.gameObject;
    116.  
    117.         if(hitObject.tag == "Resource" && task == TaskList.Gathering)
    118.         {
    119.             isGathering = true;
    120.             hitObject.GetComponent<NodeManager>().gatherers++;
    121.             heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
    122.         }else if(hitObject.tag == "Drops" && task == TaskList.Delivering)
    123.         {
    124.             if(RM.mana >= RM.maxMana)
    125.             {
    126.                 task = TaskList.Idle;
    127.             }
    128.             else
    129.             {
    130.                 RM.mana += heldResource;
    131.                 heldResource = 0;
    132.                 task = TaskList.Gathering;
    133.                 agent.destination = targetNode.transform.position;
    134.             }
    135.         }
    136.     }
    137.  
    138.     public void OnTriggerExit(Collider other)
    139.     {
    140.         GameObject hitObject = other.gameObject;
    141.  
    142.         if(hitObject.tag == "Resource") {
    143.             isGathering = false;
    144.             hitObject.GetComponent<NodeManager>().gatherers--;
    145.         }
    146.     }
    147.  
    148.  
    149.     IEnumerator GatherTick()
    150.     {
    151.         while (true)
    152.         {
    153.             yield return new WaitForSeconds(1);
    154.             if (isGathering)
    155.             {
    156.                 heldResource++;
    157.             }
    158.         }
    159.     }
    160. }
    161.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    RayCast only returns the first collider. What if it’s the own player? Better use RayCastAll, and make sure you process all collider instead of making assumptions that there only ever can be two.
     
  3. jacob_p01

    jacob_p01

    Joined:
    Nov 20, 2018
    Posts:
    8
    I get what you're saying, but then shouldn't some collider still return as the targetNode? I'm getting nothing in return, while the target of the gatherer is set correctly to the node with Nav Mesh Agent, that same collider isnt being returned to the target node
     
  4. jacob_p01

    jacob_p01

    Joined:
    Nov 20, 2018
    Posts:
    8
    So I still can't seem to figure this out. Every available option I've tried has used the same methods, but mine isn't working.