Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Avoid Overlapping Sprites. But how?

Discussion in 'Scripting' started by GabeGabe, Feb 5, 2015.

  1. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38
    This is my problem: http://i.imgur.com/6NJR048.png

    I've got a prefab called "Tapper" and at the beginning of the game the game instantiate 4 of these and then after 1 sec the transform.positions are generated randomly so it's jumping to another location. But sometimes these sprite are overlapping each other like on the image.

    How can I prevent this?
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Check to see if the radius of your new sprite is inside the radius of an existing sprite before you instantiate it.
     
  3. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38
    Okay....but how? :D
     
  4. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  5. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38
    I tried it with 2D circle colliders like this but it's not working. Why?
    The sprites are tagged as "Tapper". And this script is attached to every Tapper.

    Code (CSharp):
    1. void OnCollision2DEnter(Collider2D coll){
    2.         if (coll.gameObject.tag == "Tapper") {
    3.             this.transform.position =     new Vector3 (Random.Range (-2.45f, 2.45f), Random.Range (3.23f, -4.59f), 0f);
    4.         }
    5.     }
     
  6. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
  7. GabeGabe

    GabeGabe

    Joined:
    Jan 4, 2015
    Posts:
    38