0

Im having a problem in one of my projects in unity. I basicly have a system where you click an object and then you click somewhere inside an area and the object goes to that position. the problem is that those objects can't overlap. And they can't use physics. to detect colision with each other. I have been working on this for a long time I got something but still not totally working and I hoped someone could help me.

What im basicly doing is getting all objects near the click and then if there are some just calculate the directions between the click and those objects and then add them to the position that seems to work sometimes they don't overlap other times they do and they go to far away and I need them to be near the click.

code:

public Vector3 GetPossiblePosition(List<GameObject> nearbyDiscs, Vector3 position)
{ 
    float initialY = transform.position.y;
    Vector3 idealPosition = position;
    List<Vector3> directions = new List<Vector3>();

    if(nearbyDiscs.Count > 0)
    {
        foreach (GameObject disc in nearbyDiscs)
        {
            Vector3 newDirection = position - disc.transform.position;
            directions.Add(newDirection);
        }

        for (int i = 0; i < directions.Count; i++)
        {
            idealPosition += directions[i] / directions.Count;
            List<GameObject> discs = CheckForNearbyDiscs(idealPosition);
            if (discs.Count < 1)
                break;
        }
    }
    idealPosition.y = initialY;
    return idealPosition;
}

behaviour:

enter image description here

André Marques
  • 180
  • 1
  • 15
  • Your question is confusing. What i understand is that you have a position where the user click. Then you find all object in a certain range and you want to find the middle of all those points ? – Franck Mar 29 '18 at 13:25
  • Yes you are half right but instead of getting the middle I want to get an available position near the click to place my object @Franck – André Marques Mar 29 '18 at 13:38
  • If you really don't want to use physics, you could use a kind of grid : Each point of the grid is separated from the others by a unit greater than the size of your objects. – Greg Mar 29 '18 at 14:05
  • I had a system like that the problem is that need to do this without a grid this time and im not getting there. – André Marques Mar 29 '18 at 14:12
  • @AndréMarques So you have a grid shape you want to follow a grid positioning as in lets say we are in 2d to simplyfy and your object is at 50,50 and is 100x100 and if you click at 75,75 you want to know you are in conflict with that object but if you click at 101,50 you want the point 150,50 which extend from 100,0 to 200,100 is that it ? maybe a picture of expected result would help too. – Franck Mar 29 '18 at 14:33
  • Are the objects circular (guessing so if "discs")? What are the rules you desire to define the best placement? Say for example, you have a bunch of these shapes arranged in a circular pattern, and the space in the middle of them isn't large enough to hold a new piece - if the user tries to place the new piece in that center space, where should it go? As for actually detecting overlap, you don't need to use physics but could possibly still use colliders (as triggers) to detect overlap by attempting to place the object, detecting collision, and then trying the next best location – Rick Riensche Mar 29 '18 at 14:51
  • @Franck I have edited with an example of a wrong behaviour I hope that helps understanding. – André Marques Mar 29 '18 at 15:09
  • @RickRiensche Yes they are circular I have added an image you can see above thats a bad behaviour that I want to fix currently I have the disc detects others and applys the direction between them so that it gets like an offset but sometimes it ends on a place where there is another disc so they overlap. And sometimes beside the direction calculation it overlaps others on the first calculation. – André Marques Mar 29 '18 at 15:11
  • @AndréMarques You have 2 cases. either it can fit between all circles or not. For each circle if the vector length is smaller than both radius added you need to move your new circle away. Then use that new position and do same test with all other circles. Once all done check with all circles again if any vector are now smaller than both radius without moving the object. If no collision it will be well placed, if there is 1 or more collision then no position possible. You need to create the object elsewhere (btw radius added together represent center to center you can use anything) – Franck Mar 29 '18 at 16:16
  • @Franck but for moving them away do you doing my math is right? the way im doing by appliying the direction to move them away from the objects like in my code? – André Marques Mar 29 '18 at 17:49
  • @AndréMarques In your code ii doesn't make sense to me right now. I don't understand why there is a division by count. to me it's as simple as getting the vector then normalizing it and multiply by the required distance (center to center value) – Franck Apr 02 '18 at 11:02

1 Answers1

0

You can easily do this using Physics2D.OverlapCircleAll

public static Collider2D[] OverlapCircleAll(Vector2 point, float radius, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

this method returns an array of colliders, you can simply check overlapping if the length of the returning array is greater than one and can handle accordingly.