How to avoid overlapping of GameObject without rig

2019-08-21 03:51发布

问题:

I was looking for a way to make my gameobject not overlap to the another gameobject but all the solutions are talking about Rigidbody..

when i want to do it in script only without rigidbody, is it possible ? I have a cube with this scale (3,1,1) I make him rotate around itself but i got the overlap problem because his x scale is 3

Is there anyway to make him move and back automatically to avoid the red gameobject?

image

回答1:

you can use this method

GameObject myDraggedGO; // parent

     private IEnumerator OverLapChecker(float delay)
        {
            yield return new WaitForSeconds(delay);
            Collider2D[] colls = new Collider2D[3];
            List<int> nums = new List<int>();
            foreach (var tile in myDraggedGO.GetComponentsInChildren<Rigidbody2D>())
            {
                nums.Add(Physics2D.OverlapCircleNonAlloc(tile.transform.position, 0.5f, colls));
            }
            if (nums.Contains(2))
            {
                myDraggedGO.transform.position = _startpositionOnDrag;
            }
        }