public Vector3 GetSteeringVelocity(ICollection <GameObject> targets) { Vector3 accel = Vector3.zero; int count = 0; Rigidbody2D rb2D; foreach (GameObject r in targets) { rb2D = GetComponent <Rigidbody2D>(); Vector3 rbPos = rb2D.position; if (steeringBasics.IsFacing(rbPos, facingCosineVal)) { /* Calculate the acceleration we want to match this target */ Vector3 rbVel = rb2D.velocity; Vector3 a = rbVel - rb.Velocity; /* Rather than accelerate the character to the correct speed in 1 second, * accelerate so we reach the desired speed in timeToTarget seconds * (if we were to actually accelerate for the full timeToTarget seconds). */ a = a / timeToTarget; accel += a; count++; } } if (count > 0) { accel = accel / count; /* Make sure we are accelerating at max acceleration */ if (accel.magnitude > maxAcceleration) { accel = accel.normalized * maxAcceleration; } } return(accel); }