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

Sphere Colliders not to overlay. Pls help.

Discussion in '2D' started by superflay, Jan 27, 2016.

  1. superflay

    superflay

    Joined:
    Nov 21, 2015
    Posts:
    13
    Hello guys,

    I created two sprites that are circles basically and I added on them Rigidbody(NOT 2d) + Sphere Collider (not 2d). So when I add this little code on the prefab when the objects touch they kill eachother so I know the collider works


    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if(other.gameObject.name == target.gameObject.name)
    4.         {
    5.                        
    6.             Destroy(GameObject);
    7.         }
    8.  
    Now the issue is that I want to delete the Destroy function and make the objects not to overlay so they will get close to eachother until they touch but not go through eachother like cells.
    I am not sure how to do that.
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Try adding Rigidbody (not 2D) components to both of them. Their motion will then be supervised by the physics engine, which means they won't go through each other. It also means they will fall down (toward -y) due to gravity, so turn off gravity if you don't want that to happen, either in the Rigidbody components (Rigidbody.useGravity) or by using Physics.gravity.
     
  3. superflay

    superflay

    Joined:
    Nov 21, 2015
    Posts:
    13
    Yeah both objects have a rigidbody and a sphere collider on them and they are moving randomly in space (movement made with script) So then I click a location in space these particles get the target destination and they go to that point but I don't want them to collide, I want them to go to that point in space and not go one on top of the other.
     
  4. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
  5. superflay

    superflay

    Joined:
    Nov 21, 2015
    Posts:
    13
    Right now it looks like this (they get one on top of the other):


    I want to be like this:

    You see, they move as close as possible to eachother but never overlay...

    Once again, the circles are 2d objects (sprites) and I attached to them 3d collider (sphere collider) + 3d Rigidbody (Rigidbody).
    Also I disabled the gravity in Unity settings.
    So now when I tell the circles to go to the corner of the screen for example they just go there and overlay so in the end there is just one circle with all the rest under it (or on top) , what I want is them to go to that place but just group around.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    Don't use triggers which allow overlap. Also, if you're not interested in Z collisions then why not use 2D physics as you'll get better performance using Rigidbody2D/CircleCollider2D.

    You can then move the Rigidbody(2D) by using MovePosition to attempt to move to a position.

    If you (for some reason) don't want the physics keeping things separated then you'll need to do that yourself by checking each enter/stay callback and ensuring the separation is at least the radius of the circle/sphere collider but doing that yourself is much slower than letting the physics system do it in native code.