Keep flies in a sphere?

Hello everyone.

  1. I have a sphere. In this sphere, I instantiate random cubes (flies). And the flies are flying back and forth (trembling). Now I want, that all the flies are staying in this sphere. Leaving not the sphere. How can I do that. Please look Fig. 1:
    alt text

  2. In this example, I want that the flies fly from one sphere to another sphere. Please look Fig. 2:
    alt text

How can I solve this?

What must I search? What are the search words. Has somebody some some ideas. How can I solve that?

Vector3 LastPosition
public Vector3 SphereCenter;
public float SphereRadius;
void FixedUpdate()
{
if (Vector3.Distance(transform.position, SphereCenter) > SphereRadius)
{
rigidbody.position = lastPosition;
}
LastPosition = transform.position;
}

This will keep your flies inside your sphere at all times. To move your flies to another sphere, just change SphereCenter. If you want to make the sphere bigger, change SphereRadius.