Making a Gameobject expand!


I’ve recently started to use Unity so I am quite fresh to the experience, please bear with me.

At the moment I’m currently able to make a prefab sphere instantiate on multiple targets within my game, however I’m looking to make these spheres expand and contract. Basically, when the player spawns a sphere I want it to start to expand until it either hits the wall of the box or another sphere. When it does so, I want a random sound (out of 25) to be played on collision and for the sphere to contract. At this moment I have near to no idea of how to structure this expansion, whether I use transform.localscale, rigidbodys or animation, I am pretty stuck.

So any help and tips would be much appreciated, sorry and thanks!

Here is an image of what the project looks like so far.
alt text

You could perform a simple sphere collision test for the spheres and use a radius test for the walls for the ones that are out there on the outer faces.

Sphere collision is a simple as taking the distance between two spheres and check if it is less or equal to the sum of the two spheres radius. If it is, then the sphere has collided with another sphere. To check if a sphere is large enough to hit the wall, you could just check if the radius is greater than or equal to the distance to the wall.

Once you have detected these conditions, play your random sound. You might also want to adjust the spheres radius you are changing so it doesn’t visually overlap.

If you imagine we hit a condition for where you are increasing the radius of sphere1, consider this:

// since a collision happens when
// distance <= sphere1.radius + sphere2.radius
sphere1.radius = distance - sphere2.radius;

And likewise you could do the same for hitting a wall

sphere1.radius = distanceToWalls;