Make cubes collide with each other

Hi all,

At the moment I have a script called Follow.js, which is applied to multiple cubes. What happens is that when you hit a cube, it starts to follow you and then when it gets within two blocks of you it stops…this is working fine. But what is happening is when i stop all of the cubes following me merge into one, how would i do it so that they do not clip inside each other, and stay in their stationary positions behind me?

Follow.js:

    var target : Transform; //the enemy's target
    var moveSpeed = 3; //move speed
    var rotationSpeed = 3; //speed of turning    
    var Player = GameObject.Find("Player").transform.position;
    var Cube2 = GameObject.Find("Cube2").transform.position;
     
    var myTransform : Transform; //current transform data of this enemy
     
      
    function Update () {
    
   
    
    
    
    if (target == GameObject.FindWithTag("Player").transform)
    {
	//rotate to look at the player
	//GameObject.Find("Cube2").transform.position = GameObject.Find("Player").transform.position + 5;
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    
    var distanceToPlayer : float = Vector3.Distance(target.transform.position, myTransform.position);
    
    
    if(distanceToPlayer > 2){
     
    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    
    }
   	
   	
    }
 
     
     
    }

Thanks for any suggestions!

Instead of directly alter the transform.position, you can instead calculate th desired position (save the position and only work with that position).
After that, you apply a force from the cube to that point.

// The desired position of the cube... NOT the real position
private vector3 actualPosition = new vector3();

// Work with physics is normaly made in fixed update
Void fixedUpdate()
{
    // Calculate the new position like you did with the cube position
    // Store it in actualPosition

    // Calculate the direction from the cube to actualPosition
    // Add a force with the calculated direction to the cube
    // Don't forget to multiply the directed force with time.deltatime to 
    // exclude the framerate. (Time.deltaTime * force)
}

That should give the cubes a realistic behaviour, you could even replace the cube with each shape you want.

Hope I was able to help
Greetings
Chillersanim