What collider/s should I add to gameobjects to prevent from the player character to move through ?

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class GetComponents : MonoBehaviour
{
    public Transform parent;
    public List<GameObject> allObjects = new List<GameObject>();

    private void Start()
    {
        string path = "e:/colliders.txt";
        StreamWriter writer = new StreamWriter(path, true);

        allObjects = FindObjectsOfType<GameObject>().ToList();

        Transform[] allChildren = parent.GetComponentsInChildren<Transform>();

        foreach (Transform child in allChildren)
        {
            var colliders = child.GetComponents<Collider>();

            int length = colliders.Length;

            if (length == 0)
            {
                writer.WriteLine(string.Format("{0} - No Colliders", child.name));
            }
            else
            {
                //composes a list of the colliders types, this will print what you want e.g. "Wall1 - BoxCollider, MeshCollider"

                string colliderTypes = string.Empty;

                for (int i = 0; i < length; i++)
                {
                    colliderTypes = string.Format("{0}{1}", colliderTypes, colliders[i].GetType().Name);

                    if (i != (length - 1))
                    {
                        colliderTypes = string.Format("{0}, ", colliderTypes);
                    }
                }
                //writer.WriteLine(string.Format("{0} - {1}", child.name, colliderTypes));
            }
        }

        writer.Close();
    }
}

The result is over 1000 gameobjects without colliders.
And I want to add to them a collider to prevent from the player character to move/walk through them.

The question is what collider/s should I add to each gameobject ?

Depends on the objects. Sphere colliders are fastest, capsule is fast, box a bit slower, mesh the slowest.
Use what’s necessary depending on the shape and/ or required accuracy.

Also, if you are in the range of thousands of gameobjects (especially if those have monobehavior scripts), you may run into performance problems eventually. For such large requirements it may be eventually worth to look into data oriented design, using DOTS. With DOTS, for most intents and purposes, the CPU shouldnt ever become the bottleneck.
That said, most of the time you dont really need thousands of gameobjects, so may i ask what you require them for? There may or may not be a better solution.

1 Like

It’s a assest I bought time ago and the owner who built the example of a scifi station didn’t add colliders to all objects so the player can walk through some of the walls(objects). I’m not going to give the possible of moving all around the scifi station in my game but yet in some places it’s important to have colliders. And move now one by one to check it’s shape and what collider is needed is a bit a problem I think.

You could just add a meshcollider to all of these object and live with the performance hit, which may not even be noticable. That said, i’d personally refrain from using things you dont need. If parts of the station are just there “as background”, then you may be able to get rid of them and handle visuals differently, or at the very least combine them and make them static, to cut down on drawcalls. This too may take some time tho.

One thing that may make manual collider assignment easier, would be that on a 1k+ item scene, the author probably (hopefully) made heavy use of prefabs. So you could just take a look at what objects you are actually going to need colliders on (walls, floor, doors, maybe tables, …) and add a fitting collider to their prefab, which then adds it to every such object in the scene. I cant imagine that the scene consists of 1k+ different items, so this should drastically cut down on the work you have to do. If the author did not use any prefabs… well then you are probably out of luck on that regard.

1 Like