How to avoid overlapping of GameObject without rigidbody?

时光毁灭记忆、已成空白 提交于 2020-01-06 04:31:11

问题


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


来源:https://stackoverflow.com/questions/49496421/how-to-avoid-overlapping-of-gameobject-without-rigidbody

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!