Skip to main content
Tweeted twitter.com/StackGameDev/status/1520870685211512832
Grammar
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

How to create a boxcollider, whichbox collider that surrounds the gameobjectan object and all of hisits children?

I have a Car, whocar that needs a Box Colliderbox collider. If

If the car would bewere 1 single mesh, iI would only need to call:

and it would create a boxCollider, which fitsBoxCollider that perfectly fits my car, based on my car's mesh.

Now I have a car which existsconsists of many different parts in the transformation hierarchy. (forFor example: Bodythe body of the car is the parent of the 4 doors. Every door is 1 seperate gameobjecta separate game object, and has a doorknob child, etc.  )

now iNow I need a script whichthat changes the boxColliderBoxCollider so, that it has theits box surroundingsurrounds the whole car, including all of these parts.

i found: http://answers.unity3d.com/questions/22019/auto-sizing-primitive-collider-based-on-child-mesh.htmlI found this pose on Unity Answers

 , but it just doesntdoesn't get me the right collider.

Edit: I just tried again - and the Boxcollider staysHere is the same.code I'm using:

How to create a boxcollider, which surrounds the gameobject and all of his children?

I have a Car, who needs a Box Collider. If the car would be 1 single mesh, i would only need to call:

and it would create a boxCollider, which fits perfectly my car, based on my car's mesh.

Now I have a car which exists of many different parts in the hierarchy. (for example: Body of the car is the parent of the 4 doors. Every door is 1 seperate gameobject, and has a doorknob child etc.  )

now i need a script which changes the boxCollider so, that it has the box surrounding the whole car.

i found: http://answers.unity3d.com/questions/22019/auto-sizing-primitive-collider-based-on-child-mesh.html

  but it just doesnt get me the right collider.

Edit: I just tried again - and the Boxcollider stays the same.

How to create a box collider that surrounds an object and its children?

I have a car that needs a box collider.

If the car were 1 single mesh, I would only need to call:

and it would create a BoxCollider that perfectly fits my car, based on my car's mesh.

Now I have a car which consists of many different parts in the transformation hierarchy. (For example: the body of the car is the parent of the 4 doors. Every door is a separate game object, and has a doorknob child, etc.)

Now I need a script that changes the BoxCollider so that its box surrounds the whole car, including all of these parts.

I found this pose on Unity Answers, but it just doesn't get me the right collider.

Here is the code I'm using:

added 1506 characters in body
Source Link
OC_RaizW
  • 1.5k
  • 8
  • 27
  • 49

but it just doesnt get me the right collider.

Edit: What I get is a collider which seems to moved a bit out of the centerI just tried again - and scaled biggerthe Boxcollider stays the same.

using UnityEngine;
using UnityEditor;
using System.Collections;

public class ColliderToFit : MonoBehaviour
{

    [MenuItem("My Tools/Collider/Fit to Children")]
    static void FitToChildren()
    {
        foreach (GameObject rootGameObject in Selection.gameObjects)
        {
            if (!(rootGameObject.GetComponent<Collider>()  is BoxCollider))
                continue;

            bool hasBounds = false;
            Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);

            for (int i = 0; i < rootGameObject.transform.childCount; ++i)
            {
                Renderer childRenderer = rootGameObject.transform.GetChild(i).GetComponent<Renderer>();
                if (childRenderer != null)
                {
                    if (hasBounds)
                    {
                        bounds.Encapsulate(childRenderer.bounds);
                    }
                    else
                    {
                        bounds = childRenderer.bounds;
                        hasBounds = true;
                    }
                }
            }

            BoxCollider collider = (BoxCollider)rootGameObject.GetComponent<Collider>();
            collider.center = bounds.center - rootGameObject.transform.position;
            collider.size = bounds.size;
        }
    }

}

but it just doesnt get me the right collider. What I get is a collider which seems to moved a bit out of the center and scaled bigger.

but it just doesnt get me the right collider.

Edit: I just tried again - and the Boxcollider stays the same.

using UnityEngine;
using UnityEditor;
using System.Collections;

public class ColliderToFit : MonoBehaviour
{

    [MenuItem("My Tools/Collider/Fit to Children")]
    static void FitToChildren()
    {
        foreach (GameObject rootGameObject in Selection.gameObjects)
        {
            if (!(rootGameObject.GetComponent<Collider>()  is BoxCollider))
                continue;

            bool hasBounds = false;
            Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);

            for (int i = 0; i < rootGameObject.transform.childCount; ++i)
            {
                Renderer childRenderer = rootGameObject.transform.GetChild(i).GetComponent<Renderer>();
                if (childRenderer != null)
                {
                    if (hasBounds)
                    {
                        bounds.Encapsulate(childRenderer.bounds);
                    }
                    else
                    {
                        bounds = childRenderer.bounds;
                        hasBounds = true;
                    }
                }
            }

            BoxCollider collider = (BoxCollider)rootGameObject.GetComponent<Collider>();
            collider.center = bounds.center - rootGameObject.transform.position;
            collider.size = bounds.size;
        }
    }

}
added 169 characters in body
Source Link
OC_RaizW
  • 1.5k
  • 8
  • 27
  • 49

I have a Car, who needs a Box Collider. If the car would be 1 single mesh, i would only need to call:

        boxCol = gameObject.AddComponent<BoxCollider>();

and it would create a boxCollider, which fits perfectly my car, based on my car's mesh.

Now I have a car which exists of many different parts in the hierarchy. (for example: everyBody of the car is the parent of the 4 doors. Every door is 1 seperate game objectgameobject, and has a doorknob child etc. )

now i need a script which changes the boxCollider so, that it has the box surrounding the wholecarwhole car.

i found: http://answers.unity3d.com/questions/22019/auto-sizing-primitive-collider-based-on-child-mesh.html

but it just doesnt get me the right collider. What I get is a collider which seems to moved a bit out of the center and scaled bigger.

I have a Car, who needs a Box Collider. If the car would be 1 single mesh, i would only need to call:

        boxCol = gameObject.AddComponent<BoxCollider>();

and it would create a boxCollider, which fits perfectly my car, based on my car's mesh.

Now I have a car which exists of many different parts in the hierarchy. (for example: every door is 1 seperate game object)

now i need a script which changes the boxCollider so, that it has the box surrounding the wholecar.

i found: http://answers.unity3d.com/questions/22019/auto-sizing-primitive-collider-based-on-child-mesh.html

but it just doesnt get me the right collider

I have a Car, who needs a Box Collider. If the car would be 1 single mesh, i would only need to call:

        boxCol = gameObject.AddComponent<BoxCollider>();

and it would create a boxCollider, which fits perfectly my car, based on my car's mesh.

Now I have a car which exists of many different parts in the hierarchy. (for example: Body of the car is the parent of the 4 doors. Every door is 1 seperate gameobject, and has a doorknob child etc. )

now i need a script which changes the boxCollider so, that it has the box surrounding the whole car.

i found: http://answers.unity3d.com/questions/22019/auto-sizing-primitive-collider-based-on-child-mesh.html

but it just doesnt get me the right collider. What I get is a collider which seems to moved a bit out of the center and scaled bigger.

Source Link
OC_RaizW
  • 1.5k
  • 8
  • 27
  • 49
Loading