Example #1
0
    public Vector2 MoveWithGameObjects(Vector2 delta, List <GameObject> attachedObjects)
    {
        gameObjectsToIgnoreCollisonsWith.AddRange(attachedObjects);
        Vector2 newDelta = new Vector2(delta.x, delta.y);

        if (delta.x != 0)
        {
            newDelta.x = CalculateMoveX(delta);
        }
        if (delta.y != 0)
        {
            newDelta.y = CalculateMoveY(delta);
        }

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
            mcs.gameObjectsToIgnoreCollisonsWith.Add(gameObject);
            Vector2 objectDelta = new Vector2(0, 0);
            if (delta.x != 0)
            {
                objectDelta.x = mcs.CalculateMoveX(delta);
            }
            if (delta.y != 0)
            {
                objectDelta.y = mcs.CalculateMoveY(delta);
            }

            if (Mathf.Abs(objectDelta.x) < Mathf.Abs(newDelta.x))
            {
                newDelta.x = objectDelta.x;
            }
            if (Mathf.Abs(objectDelta.y) < Mathf.Abs(newDelta.y))
            {
                newDelta.y = objectDelta.y;
            }
        }

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
            mcs.gameObjectsToIgnoreCollisonsWith.Remove(gameObject);
            go.transform.Translate(newDelta);
        }

        gameObjectsToIgnoreCollisonsWith = gameObjectsToIgnoreCollisonsWith.Except <GameObject>(attachedObjects).ToList <GameObject>();
        transform.Translate(newDelta);
        return(newDelta);
    }
    public Vector2 CalculateMoveWithGameObjects(Vector2 delta, List <GameObject> attachedObjects)
    {
        List <GameObject> tmpList = new List <GameObject>(gameObjectsToIgnoreCollisonsWith);

        gameObjectsToIgnoreCollisonsWith.AddRange(attachedObjects);
        Vector2 newDelta = new Vector2(delta.x, delta.y);

        if (delta.x != 0)
        {
            newDelta.x = CalculateMoveX(delta);
        }
        if (delta.y != 0)
        {
            newDelta.y = CalculateMoveY(delta);
        }

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs      = go.GetComponent <MovementControllerScript>();
            List <GameObject>        tmpList2 = new List <GameObject>(mcs.gameObjectsToIgnoreCollisonsWith);
            mcs.gameObjectsToIgnoreCollisonsWith.Add(gameObject);
            mcs.gameObjectsToIgnoreCollisonsWith.AddRange(attachedObjects);

            Vector2 objectDelta = new Vector2(0, 0);
            if (delta.x != 0)
            {
                objectDelta.x = mcs.CalculateMoveX(delta);
            }
            if (delta.y != 0)
            {
                objectDelta.y = mcs.CalculateMoveY(delta);
            }

            if (Mathf.Abs(objectDelta.x) < Mathf.Abs(newDelta.x))
            {
                newDelta.x = objectDelta.x;
            }
            if (Mathf.Abs(objectDelta.y) < Mathf.Abs(newDelta.y))
            {
                newDelta.y = objectDelta.y;
            }
            mcs.gameObjectsToIgnoreCollisonsWith = tmpList2;
        }

        gameObjectsToIgnoreCollisonsWith = tmpList;
        return(newDelta);
    }
    public float CalculateMoveWithGameObjectsY(Vector2 delta, List <GameObject> attachedObjects)
    {
        float distance = CalculateMoveY(delta, attachedObjects);

        List <GameObject> objectsToIgnore = new List <GameObject>();

        objectsToIgnore.Add(gameObject);

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
            if (mcs != null)
            {
                float testDistance = mcs.CalculateMoveY(delta, objectsToIgnore);
                if (Mathf.Abs(testDistance) < Mathf.Abs(distance))
                {
                    distance = testDistance;
                }
            }
        }
        return(distance * Mathf.Sign(delta.y));
    }