-1

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));
    }
}
Vaillancourt
  • 16,325
  • 17
  • 55
  • 61
Adnan
  • 1
  • 1

1 Answers1

0

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);
}

}
Omer
  • 214
  • 4
  • 17
  • 1
    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 '19 at 13:00