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.
public void RightClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.tag == "Ground")
{
agent.destination = hit.point;
Debug.Log("Moving");
task = TaskList.Moving;
}
else if(hit.collider.tag == "Resource")
{
agent.destination = hit.collider.gameObject.transform.position;
Debug.Log("Harvesting");
task = TaskList.Gathering;
targetNode = hit.collider.gameObject;
}
}
}
If you need more insight into what is happening in the script, here is the full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ObjectInfo : MonoBehaviour
{
public TaskList task;
public ResourceManager RM;
public GameObject targetNode;
public NodeManager.ResourceTypes heldResourceType;
public bool isSelected = false;
public bool isGathering = false;
public string objectName;
private NavMeshAgent agent;
public int heldResource;
public int maxHeldResource;
GameObject[] drops;
// Start is called before the first frame update
void Start()
{
StartCoroutine(GatherTick());
agent = GetComponent<NavMeshAgent>();
}
// 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) && 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")
{
agent.destination = hit.point;
Debug.Log("Moving");
task = TaskList.Moving;
}
else if(hit.collider.tag == "Resource")
{
agent.destination = hit.collider.gameObject.transform.position;
Debug.Log("Harvesting");
task = TaskList.Gathering;
targetNode = hit.collider.gameObject;
}
}
}
public void OnTriggerEnter(Collider other)
{
GameObject hitObject = other.gameObject;
if(hitObject.tag == "Resource" && task == TaskList.Gathering)
{
isGathering = true;
hitObject.GetComponent<NodeManager>().gatherers++;
heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
}else if(hitObject.tag == "Drops" && task == TaskList.Delivering)
{
if(RM.mana >= RM.maxMana)
{
task = TaskList.Idle;
}
else
{
RM.mana += heldResource;
heldResource = 0;
task = TaskList.Gathering;
agent.destination = targetNode.transform.position;
}
}
}
public void OnTriggerExit(Collider other)
{
GameObject hitObject = other.gameObject;
if(hitObject.tag == "Resource") {
isGathering = false;
hitObject.GetComponent<NodeManager>().gatherers--;
}
}
IEnumerator GatherTick()
{
while (true)
{
yield return new WaitForSeconds(1);
if (isGathering)
{
heldResource++;
}
}
}
}