0

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.

External image link

Inline image two

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;
}
jtth
  • 876
  • 1
  • 12
  • 40
  • 1
    As I see it the problem doesnt lie with lerp, the problem is that you are moving the objects to the same distance from child, and thats what they are doing, if you want for distance between them to grow you got to pass different coordinates for them to lerp. – Arman Papikyan Nov 14 '17 at 18:52
  • @ArmanPapikyan "[P]ass different coordinates for them [child objects] to lerp". How would you suggest going about this? The objects first and foremost need to be outside of each others collider.bounds (collision boundaries), then some sort of minimum distance implemented? – jtth Nov 15 '17 at 20:35
  • You fixed the title and I have reopened the question. I hope you find your answer from others. – Programmer Nov 16 '17 at 20:27

0 Answers0