void tryfetch() // todo: implement a sub method to simplify this one { GameObject target = pickuptarget; // todo: if on top of pickup, move to the side // if fetch target exists if (timeSpentFetching < 5 && pickuptarget && pickupCarryIndexAndPositionOffset.Key != -1) { PickupController targetController = target.GetComponentInChildren <PickupController>(); // if at carry point location if (Vector3.Distance(this.transform.position, target.transform.position + pickupCarryIndexAndPositionOffset.Value) < 1)//Vector3.Distance(target.transform.position, this.transform.position) < target.transform.localScale.x) { if (targetController) { // try to assign agent to carry point if (targetController.assignAgentToCarryPoint(this.gameObject, pickupCarryIndexAndPositionOffset.Key)) { if (agentcontroller) { // todo: need to locate the right sound effect // agentcontroller.soundPlayer.clip = agentcontroller.holdClip; // agentcontroller.soundPlayer.Play(); } agentcontroller.agentState = AgentState.Holding; agentDestination = target.transform.position + pickupCarryIndexAndPositionOffset.Value; if (targetController.checkIfCarryable()) // if enough agents carrying, start moving { dopickup(target); } } else // if another agent took the spot already { if (targetController.maxAgentsReached()) // if no free spots left, give up { // todo: 'give up' animation if (agentcontroller) { // todo: need to locate the right sound effect //agentcontroller.soundPlayer.clip = agentcontroller.giveupClip; //agentcontroller.soundPlayer.Play(); } pickuptarget = null; pickupCarryIndexAndPositionOffset = new KeyValuePair <int, Vector3>(-1, new Vector3(0, 0, 0)); agentnav.stoppingDistance = navStopDistanceBackup; } else // otherwise get new target carry point { pickupCarryIndexAndPositionOffset = targetController.getFreeCarryIndexAndOffset(this.gameObject); // todo: give up after failing a number of times agentDestination = target.transform.position + pickupCarryIndexAndPositionOffset.Value; } } } } else // not at carry point location yet { //Debug.Log("pik is approaching pickup. distance: " + Vector3.Distance(this.transform.position, target.transform.position + pickupCarryIndexAndPositionOffset.Value) + " with required distance 1"); // check whether carry point was taken already - if so, get new carry point if (targetController.checkIfCarryPointOccupied(pickupCarryIndexAndPositionOffset.Key)) { pickupCarryIndexAndPositionOffset = targetController.getFreeCarryIndexAndOffset(this.gameObject); // todo: give up after failing a number of times } // approach carry point agentDestination = target.transform.position + pickupCarryIndexAndPositionOffset.Value; } } else if (timeSpentFetching >= 5 || !pickuptarget) { resetNav(); agentcontroller.dismiss(); timeSpentFetching = 0; } }