}//end Awake void Update () { target = tS.target;//set the target. this saves referencing it in the trader script each time if (target == null){ if(tS.finalPost != null) target = tS.target = tS.finalPost.gameObject;//if not heading for anything, then go to the final post else target = tS.target = tS.startPost.gameObject;//if final post gone, go to start post }//end if target null //the line above is in case the item being collected has been collected by another trader if (tS.onCall && tS.allowGo) {//if these are both true, then the trader is allowed to move //replace the next two lines with your movement AI with the destination set to be the target transform.LookAt (target.transform.position);//look at where we want to go transform.Translate (Vector3.forward * Time.timeScale * .05f);//move towards the target if (collect && tS.allowCollect) {//only needs to check for dropped items if is set in the controller and on the trader itemsInRadar = Physics.OverlapSphere (transform.position, radarDistance);//get the objects within the radar distance GameObject cItem = null;//the closest gameobject of the collider float cDist = Mathf.Infinity;//the distance that the closest object is away for (int c = 0; c<itemsInRadar.Length; c++) {//go through all items in radar GameObject item = itemsInRadar [c].gameObject;//the gameobject that is being checked if (item.tag == CallumP.TradeSys.Tags.I) {//if the tag of the gameobject is item CallumP.TradeSys.Item iS = item.GetComponent<CallumP.TradeSys.Item> ();//get the item script if (iS.traderCollect && tS.spaceRemaining >= (iS.number * controller.goods [iS.groupID].goods [iS.itemID].mass)) { //check that the trader can collect the item and has enough cargo space for this item float dist = (transform.position - item.transform.position).sqrMagnitude;//get the magnitude of the distance away. uses sqrmagnitude as the actual distance is not required if (dist < cDist) {//if is the closest item cDist = dist;//set the closest distance cItem = item;//set the item }//end is the closest item }//end space check }//end if is item }//end for all items //now have the closest item to the trader, so need to tell the trader to go to it if (cItem != null) target = tS.target = cItem;//set the target to be the closest item }//end if collection allowed if (target != null && Vector3.Distance (transform.position, target.transform.position) <= tS.closeDistance) { //is close enough if (target.gameObject.tag == CallumP.TradeSys.Tags.TP)//if the target is a trade post StartCoroutine (tS.AtPost ());//IMPORTANT - call the AtPost method, so will unload the cargo else//else the target is an item, so needs to be picked up StartCoroutine (CollectItem ());//collect the item }//end if close enough }//end if able to go }//end Update
}//end Update IEnumerator CollectItem () {//collect the item tS.allowGo = false;//stop the trader moving yield return new WaitForSeconds (droneTime);//pause for the set time tS.allowGo = true;//trader can now continue if (target != null) { CallumP.TradeSys.Item iS = target.GetComponent<CallumP.TradeSys.Item> ();//get the item script if(iS != null){//check if null as it may have been collected tS.items [iS.groupID].items [iS.itemID].number += iS.number;//increase the number being carreid tS.spaceRemaining -= iS.number * controller.goods [iS.groupID].goods [iS.itemID].mass;//decrease the cargo space remaining iS.Collected ();//IMPORTANT - need to say that the item has been collected so the spawner count can be updated }//end if not null }//end if target not null }//end CollectItem
}//end Update bool CheckPos() {//Check if there are any trade posts within close distance Collider[] nearbyObjects = Physics.OverlapSphere(this.transform.position, closeDistance); for (int n = 0; n < nearbyObjects.Length; n++) {//go through nearby objects and see if they have the trade post tag if (nearbyObjects[n].tag == CallumP.TradeSys.Tags.TP) {//check has the trade post tag nearPost = nearbyObjects[n].GetComponent<CallumP.TradeSys.TradePost>();//set the near post to the trade post script return true;//return true so doesnt go through the rest of the nearby objects } else if (nearbyObjects[n].tag == CallumP.TradeSys.Tags.I && controller.pickUp) {//if item tag and allowed to collect nearItem = nearbyObjects[n].GetComponent<CallumP.TradeSys.Item>();//set the near item to this return false;//needs to return false so is not seen to be at a trade post }//end if item }//end for all nearby objects return false;//return false as has not found anything }//end CheckPos