There are two parts to this problem - the first being that a nested for loop is run to separate the children of an parent game object via their collision boundaries (collider.bounds). If there is a collision (boundaries intersect) the intersecting game object executes the Lerp code in an attempt to exit the collision.
Lerp executes properly but, as seen in the image, it isn't functioning correctly. It moves children 1,2,3,4 outside of each others boundaries correctly, but it moves them entirely too far from child 0. Likewise, if the duration is changed, the time to move each object is adjusted but so is the distance between them - this shouldn't be so.
EDIT: (additional detail)
Function passes an index through parameters rather then Transform and Vector3. Transform fromPosition, Vector3 toPosition and startPos are all used in the same fashion but, rather then passing them in, as Programmers coroutine originally does, (as I can't because this implementation is run-time specific and I don't know which objects exactly this will be running on) they are calculated first thing inside the modified co-routine.
Programmers implementation is moving a game object to a specified location. I need to Lerp each child until they aren't intersecting with each others bounds.
What this means is Lerp needs to be continually executed with the specification that the endPosition be, once the object its moving is no longer intersecting with the object its comparing. The way it is implemented here is using a while loop to continually execute a Lerp with gradual movement (Vector3.right) as the endPosition, until the colliders no longer intersect. Problem is, it is not working as intended.
So, how to fix the Lerp implementation here?
Resources below:
Inlined image, link to this image, relevant code
P.S.:
- The code under the comment '//translating' is to show what the desired result is, except that translating does not produce smooth movement like Lerp.
- Coroutine, 'MoveToX', is almost entirely code reused from a similar question on SE, in which the user asks how to Lerp smoothly outside of update.
int numChildren = gameObject.transform.childCount;
for (int i = 0; i<numChildren; i++)
{
for (int a = i+1; a<numChildren; a++)
{
while (gameObject.transform.GetChild(a).GetComponentInChildren<Collider>().bounds.Intersects
(gameObject.transform.GetChild(i).GetComponentInChildren<Collider>().bounds))
{
// print
Debug.Log("Bounds intersecting " + i + ":" + a);
// translating
//gameObject.transform.GetChild(a).GetComponentInChildren<Transform>()
// .transform.Translate(Vector3.right* Time.deltaTime* 100f, Space.World);
// lerping
StartCoroutine(MoveToX(a, 0.1f));
}
}
}
IEnumerator MoveToX(int a, float duration)
{
//Get the current position of the object to be moved
Vector3 startPos = gameObject.transform.GetChild(a).transform.position;
Vector3 toPosition = gameObject.transform.GetChild(a).transform.position + Vector3.right;
//Make sure there is only one instance of this function running
if (isMoving)
{
yield break; ///exit if this is still running
}
isMoving = true;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
gameObject.transform.GetChild(a).transform.position = Vector3.Lerp(startPos, toPosition, counter / duration);
yield return null;
}
isMoving = false;
}