# How can I prevent the balls from overlapping?

I am making a ball rolling game controlled by tilting an accelerometer.

In my game multiple balls overlap, so they end up showing as only one ball. How can I solve this problem, so balls do not overlap?

I have write this script:

public class Accelerometer : MonoBehaviour {
public float Speed=0.5f;
public float Speedy = 8f;
public float HorizantalMin;
public float HorizantalMax;
public float VerticalMin;
public float VerticalMax;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
float Horizental = Input.acceleration.x*Speed;
float UP = Input.acceleration.y*Speedy;
transform.Translate (Horizental, 0f,UP*Time.smoothDeltaTime);

this.transform.position = new Vector3 (Mathf.Clamp (this.transform.position.x, HorizantalMin, HorizantalMax), this.transform.position.y, this.transform.position.z);
this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y,Mathf.Clamp (this.transform.position.z, VerticalMin,VerticalMax));
}
}

• Show us what you've done so far, and how the results differ from what you want. – DMGregory Jan 9 at 17:19
• sir in my game multiple balls merge into one ball at runtime through Accelerometer – Adnan Jan 9 at 17:28
• How did you set up the colliders and rigidbodies of your balls? – Philipp Jan 9 at 17:52
• – DMGregory Jan 9 at 18:04
• I know your English teacher probably taught that "sir" is a sign of respect, but over-use can actually come across as patronizing. It"s also best not to assume that the only users who might help you are men. Lastly, you don't need to restate your question in a comment. If you feel your question is unclear, edit the question itself. – DMGregory Jan 9 at 18:33

Try this and make sure trigger is ON. this script attached on one ball

public float Speed=0.5f;
public float Speedy = 8f;
public float HorizantalMin;
public float HorizantalMax;
public float VerticalMin;
public float VerticalMax;

public GameObject second_ball;
public float disance_between_balls;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
float Horizental = Input.acceleration.x*Speed;
float UP = Input.acceleration.y*Speedy;
transform.Translate (Horizental, 0f,UP*Time.smoothDeltaTime);

this.transform.position = new Vector3 (Mathf.Clamp (this.transform.position.x, HorizantalMin, HorizantalMax), this.transform.position.y, this.transform.position.z);
this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y,Mathf.Clamp (this.transform.position.z, VerticalMin,VerticalMax));

disance_between_balls = Vector3.Distance(transform.position,second_ball.transform.position);

if (disance_between_balls < 0.5f)  // check if this distance good or not
{
second_ball.SetActive(false);
}

}

• This looks like a method to disable & hide the second ball when they get close, to make it look like they've merged into one. From my reading of the question, it looks like OP wants to avoid letting the balls merge, and wants them to collide or bounce off of each other instead. – DMGregory Jan 10 at 13:00