-1
\$\begingroup\$

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));
    }
}
\$\endgroup\$
  • \$\begingroup\$ Show us what you've done so far, and how the results differ from what you want. \$\endgroup\$ – DMGregory Jan 9 at 17:19
  • \$\begingroup\$ sir in my game multiple balls merge into one ball at runtime through Accelerometer \$\endgroup\$ – Adnan Jan 9 at 17:28
  • \$\begingroup\$ How did you set up the colliders and rigidbodies of your balls? \$\endgroup\$ – Philipp Jan 9 at 17:52
  • 1
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jan 9 at 18:33
0
\$\begingroup\$

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

}
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jan 10 at 13:00

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.