RTS character controller
public TaskList task;
public NodeManager.ResourceTypes heldResourceType;
public ResourceManager RM;
public bool isGathering;
public int heldResource;
public int maxHeldResource;
public GameObject[] drops;
private ActionList AL;
private Animator mAnimator;
private bool mRunning = false;
private NavMeshAgent mNaveMeshAgent;
GameObject targetNode;
void Start()
{
mAnimator = GetComponent<Animator>();
mNaveMeshAgent = GetComponent<NavMeshAgent>();
StartCoroutine(GatherTick());
AL = FindObjectOfType<ActionList>();
}
void Update()
{
if (targetNode == null)
{
if (heldResource != 0)
{
drops = GameObject.FindGameObjectsWithTag("Drops");
mNaveMeshAgent.destination = GetClosestDropOff(drops).transform.position;
drops = null;
task = TaskList.Delivering;
}
else
{
task = TaskList.Idle;
}
}
if (heldResource >= maxHeldResource)
{
drops = GameObject.FindGameObjectsWithTag("Drops");
mNaveMeshAgent.destination = GetClosestDropOff(drops).transform.position;
drops = null;
task = TaskList.Delivering;
}
if (Input.GetMouseButtonDown(1) && GetComponent<Player>().isSelected)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.tag == "GroundLayer")
{
AL.Move(mNaveMeshAgent, hit);
task = TaskList.Moving;
}
else if (hit.collider.tag == "Resource")
{
AL.Move(mNaveMeshAgent, hit);
targetNode = hit.collider.gameObject;
task = TaskList.Gahtering;
}
}
if (mNaveMeshAgent.remainingDistance <= mNaveMeshAgent.stoppingDistance)
{
mRunning = false;
}
else
{
mRunning = true;
}
mAnimator.SetBool("running", mRunning);
}
}
GameObject GetClosestDropOff(GameObject[] dropOffs) { GameObject closestDrop = null; float closestDistance = $$anonymous$$athf.Infinity; Vector3 position = transform.position; foreach (GameObject targetDrop in dropOffs) { Vector3 direction = targetDrop.transform.position - position; float distance = direction.sqr$$anonymous$$agnitude; if(distance < closestDistance) { closestDistance = distance; closestDrop = targetDrop; } } return closestDrop; } public void OnTriggerEnter(Collider other) { GameObject hitObject = other.gameObject;
if (hitObject.tag == "Resource" && task == TaskList.Gahtering)
{
isGathering = true;
hitObject.GetComponent<Node$$anonymous$$anager>().beingGathered++;
heldResourceType = hitObject.GetComponent<Node$$anonymous$$anager>().resourceType;
}
else if(hitObject.tag == "Drops" && task == TaskList.Delivering)
{
if(R$$anonymous$$.energy >= R$$anonymous$$.maxEnergy)
{
task = TaskList.Idle;
}
else
{
R$$anonymous$$.energy += heldResource;
heldResource = 0;
task = TaskList.Gahtering;
mNave$$anonymous$$eshAgent.destination = targetNode.transform.position;
}
}
}
public void OnTriggerExit(Collider other)
{
GameObject hitObject = other.gameObject;
if (hitObject.tag == "Resource")
{
hitObject.GetComponent<Node$$anonymous$$anager>().beingGathered--;
isGathering = false;
}
}
IEnumerator GatherTick()
{
while (true)
{
yield return new WaitForSeconds(1);
if (isGathering)
{
heldResource++;
}
}
}
}
Answer by Omirserik · Jul 15, 2020 at 04:44 PM
Hey guys, I am trying to create RTS character controller (build, select, gather resources and ability to move unit as well) Unfortunately character wouldnt move, so it cant really gather or deliver resources. I dont know whether its animator issues or RTS camera, because before my character could move perfectly fine.