Topic: Building Colliders

I'm having trouble detecting the colliders attached to buildings on the map. I want to avoid them when I spawn gameobjects. Here're the relevant procedures:

    public Vector3 CoordsToPosition(Vector2 coords)
    {
        m_map.GetCorners(out m_tlx, out m_tly, out m_brx, out m_bry);
        Vector3 world_pos = m_tileset.GetWorldPositionWithElevation(coords, m_tlx, m_tly, m_brx, m_bry);
        return world_pos;

    }

    public void SmartSpawnGameObject(GameObject prefab, Vector2 map_coords)
    {

        m_coords = map_coords;
        m_prefab = prefab;
        Vector3 world_pos = CoordsToPosition(m_coords);

        Collider[] overlaps = Physics.OverlapSphere(world_pos, m_spawn_size);
        foreach (Collider hit in overlaps)
        {
            Debug.Log(hit.gameObject.name);
        }
        // If there are no colliders in the area the unit will spawn...
        if (overlaps.Length < 2)
            Instantiate(prefab, world_pos, Quaternion.identity);
        else
        // Note that we've got to iterate through a spiral of potential spawnpoints
        {
            m_waitspawn = true;
        }
    }

Right now all I'm getting back is the Tileset's collider. I check to see if there's more than one collider since right now I can't mask the Tileset from the OverlapSphere check.

Also, for when I get that sorted, is there a relatively easy way to put the buildings on a separate layer from the map? As it stands all buildings inherit the map's layer, and since there's no prefab for the buildings, it doesn't seem to be able to be specified ahead of time. Might be a good public property to add to the Buildings component.

Since you helped me with the code to determine when all the buildings have been created, I can go in after that and find them all and set the layer that way, but thought I'd ask in case there were a more elegant solution that didn't require changing the code in your component.

Thanks!

Re: Building Colliders

Hello.

Example of how to change a layer of buildings:

using UnityEngine;

public class ChangeLayerOfBuilding:MonoBehaviour
{
    public int buildingLayer = 8;

    private void Start()
    {
        OnlineMapsBuildings.instance.OnBuildingCreated += OnBuildingCreated;
    }

    private void OnBuildingCreated(OnlineMapsBuildingBase building)
    {
        building.gameObject.layer = buildingLayer;
    }
}

Yes, I think the ability to specify a layer of buildings is useful, and we will implement it in the next version.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Building Colliders

Awesome, thanks!

I guess there are no obvious bugs in the code I pasted?

Re: Building Colliders

In general, it looks well.
But I did not test your code, so I can not say exactly.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Building Colliders

Okay, thanks. I'll figure it out and, if it's something I think others might run into, I'll post about it here.

Re: Building Colliders

My OverlapSphere was too small given the scale of the other geometry. RayCasts were detecting the colliders for the buildings, but an OverlapSphere on the surface of the map wasn't hitting the walls or roof of the building mesh. Looks like I can use OverlapCapsule instead, with the radius being the same, but scaling the height way up so it's sure to intersect the roof.

Is there a call similar to OverlapSphere that will instead tell you if the sphere is inside a collider instead of just when it's intersecting one of its edges?

Re: Building Colliders

You can check if bounds.min and bounds.max are within your sphere, then the entire collider inside the sphere.
https://docs.unity3d.com/ScriptReference/Bounds.html

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.