> For the complete documentation index, see [llms.txt](https://steijn.gitbook.io/devlogs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://steijn.gitbook.io/devlogs/blewscreen/everlake/detection.md).

# Detection

## Goal

To be able to detect whether an object could be placed, we would need a detection system. Because I have some previous knowledge of this, I knew it would consist of a (box)collider, a rigid body to be able to collide as well as a script to manage it.

## Setup

First, I needed to make sure the furniture that was going to be placed was set up properly. It always had the furniture layer and tag as well as a parent object containing the ChatroomAssetObject script.

I made sure all objects had this structure before implementing the placement.

### Detection Object

I started by adding a new gameobject to the ghost that would be the detector with the required components. &#x20;

```csharp
 GameObject detection = new("detector",
                                typeof(BoxCollider),
                                typeof(Rigidbody),
                                typeof(PlacementDection)
                                )
        {
            layer = ParentObject.layer,
            tag = ParentObject.tag,
        };
```

After this, I used another method to set the detection object to the right settings as seen below. The box collider needed to be a trigger box to be able to detect things without having a collision. The rigidbody is set to kinematic so it gets ignored by the physics collisions.

For the detector to work on differently sized objects, I would need to find a way to identify the size of the object. After some searches, I managed to get a [result](https://forum.unity.com/threads/calculating-a-bound-of-a-grouped-model.101121/) that gave me the answer I was looking for. If you go through all the Renderers, you can encapsulate them inside of a new Bounds class. This will create a box that has all the Renderers inside of it.

I ended up reducing the size of the collider by 10%. This gave the player more leeway while placing objects and caused a better overall experience.

```csharp
 private void SetDetectionObject(GameObject detection, GameObject parentObject)
 {
     detection.GetComponent<Rigidbody>().isKinematic = true;

     BoxCollider boxCollider = detection.GetComponent<BoxCollider>();
     boxCollider.isTrigger = true;
     Renderer[] parentMeshList = parentObject.GetComponentsInChildren<Renderer>();
     Bounds bounds = new();
     foreach (Renderer render in parentMeshList)
     {
         if (render != boxCollider)
         {
             bounds.Encapsulate(render.bounds);
         }
     }
     boxCollider.size = bounds.size * 0.9f;
     detection.transform.localPosition = bounds.center;
```
