// Update is called once per frame void Update() { if (gameObject.GetComponent <Renderer>().isVisible&& OnlyMoveWhenUnseen) { //Debug.Log("IS VISIBLE"); agent.speed = 0.0f; agent.velocity = Vector3.zero; CurrentGoal = StackerGoal.FreezeWhenSeen; agent.destination = pile.position; } else { //NOT VISIBLE agent.speed = base_speed; } if (CurrentGoal == StackerGoal.PickTargetObject) { //chooses the closest game object to the agent Goal_Object = GetClosestObjectViaPathing(GameObject.FindGameObjectsWithTag("CanPickUp")); //TODO only choose an object if it can be pathed to if (Goal_Object != null) { CurrentGoal = StackerGoal.PathToObject; } } else if (CurrentGoal == StackerGoal.PathToObject) { //sqrt is computationally costly, so sqrMagnitude bool NearPile = Math.Pow(VisionRange, 2) > (pile.position - transform.position).sqrMagnitude; bool NearTarget = Math.Pow(VisionRange, 2) > (Goal_Object.transform.position - transform.position).sqrMagnitude; if (ObjectWithinVisionDistance() && !NearPile && !NearTarget) { Goal_Object = GetClosestObjectViaPathing(GameObject.FindGameObjectsWithTag("CanPickUp")); //Debug.Log("SEARCHING FOR CLOSEST OBJECT"); Searching = true; } else { //Debug.Log("nominal"); Searching = false; } agent.destination = Goal_Object.transform.position; Vector3 offset = transform.position - Goal_Object.transform.position; if (offset.magnitude < PickUpRange) { IsHolding = true; CurrentGoal = StackerGoal.PathToPile; } } /* * else if (CurrentGoal == StackerGoal.PickUpObject) * { * * CurrentGoal = StackerGoal.PathToPile; * } */ else if (CurrentGoal == StackerGoal.PathToPile) { if (IsHolding) { Goal_Object.GetComponent <Rigidbody>().AddForce((transform.TransformPoint(Vector3.forward * CarryPositionOffset) - Goal_Object.transform.position) * 300); Goal_Object.GetComponent <Rigidbody>().rotation = transform.rotation; Goal_Object.GetComponent <Rigidbody>().drag = 15; Goal_Object.GetComponent <Rigidbody>().angularDrag = 15; } Vector3 offset = transform.position - pile.position; agent.destination = pile.position; if (offset.magnitude < PickUpRange) { IsHolding = false; Goal_Object.GetComponent <Rigidbody>().drag = .5f; Goal_Object.GetComponent <Rigidbody>().angularDrag = .05f; CurrentGoal = StackerGoal.PickTargetObject; } } else if (CurrentGoal == StackerGoal.FreezeWhenSeen) { Vector3 offset = transform.position - pile.position; if (offset.magnitude < PickUpRange) { CurrentGoal = StackerGoal.PickTargetObject; } } for (int i = 0; i < agent.path.corners.Length - 1; i++) { Debug.DrawLine(agent.path.corners[i], agent.path.corners[i + 1]); } }
// Use this for initialization void Start() { CurrentGoal = StackerGoal.PickTargetObject; agent = GetComponent <NavMeshAgent>(); base_speed = agent.speed; }