public static dogBehavior getClosestActiveDog(Vector3 loc) { if (instance.dogArr == null) { return(null); } float dist, minDist = -1; dogBehavior retVal = null; foreach (dogBehavior dog in instance.dogArr) { //Debug.Log ("dog is " + dog.name); dist = (dog.gameObject.transform.position - loc).magnitude; //Debug.Log ("dog.DogActive: " + dog.DogActive + " / dist is " + dist + dog.DogActive + " / dogDistanceCutoff is: " + dogDistanceCutoff + " / minDist is:" + minDist); if (dog.DogActive == true && dist < dogDistanceCutoff && (minDist == -1 || dist < minDist)) { retVal = dog; minDist = dist; } } return(retVal); }
void FixedUpdate() { if (vC.getDetected() == true || playerSeen == true) { playerSeen = true; rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; anim.SetFloat("Speed", 0f); transform.LookAt(target.transform.position); if (deathDelayTimer == 0f) { audioSrc.clip = audioArr [0]; //sound clip of laser agent.isStopped = true; //agent.enabled = false; lb.LaserOn(target); movementManager.instance.canMove = false; } else if (deathDelayTimer > deathTriggerLimit) { gameManager.death(); } deathDelayTimer += Time.fixedDeltaTime; } else { closestDog = sceneManager.getClosestActiveDog(gameObject.transform.position); //If there is a dog nearby the Android follows the dog, otherwise it navigates along its set waypoints if (closestDog != null) { agent.destination = closestDog.transform.position; rb.velocity = rb.velocity.normalized / 1.5f; } else { //Android slows down and then stops completely as it approaches its waypoint if (agent.remainingDistance > 2f) { rb.velocity = rb.velocity.normalized / 2f; waypointUpdated = false; } else if (agent.remainingDistance <= 2f && agent.remainingDistance > 0.2f) { rb.velocity = rb.velocity.normalized * Mathf.Min(agent.remainingDistance / 4 + 0.1f, 1); } else if (agent.remainingDistance <= 0.2f && waypointUpdated == false) { Debug.Log(agent.name + " is too close to destination!"); rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; GotoNextWayPoint(); waypointUpdated = true; } } anim.SetFloat("Speed", rb.velocity.magnitude); // set our animator's float parameter 'Speed' equal to the vertical input axis } }