public static GameObject CreateChildNavMeshObs(GameObject forObject, bool forPosition) { string navMeshObsName = ""; var navMeshObstacle = forObject.GetComponent <NavMeshObstacle>(); if (navMeshObstacle == null) { return(null); } navMeshObsName = forObject.name + "_NavMeshObstacle"; navMeshObsName = "**" + navMeshObsName + "_DON'T DELETE**"; GameObject childNavMeshObs = UtilityServices.DuplicateGameObject(forObject, navMeshObsName, false, false); foreach (Component component in childNavMeshObs.GetComponents <Component>()) { if (component is Transform || component is NavMeshObstacle) { continue; } DestroyImmediate(component); } navMeshObstacle.enabled = false; childNavMeshObs.transform.position = forObject.transform.position; childNavMeshObs.transform.rotation = forObject.transform.rotation; childNavMeshObs.transform.parent = forObject.transform; childNavMeshObs.AddComponent <NavObsRecognize>(); return(childNavMeshObs); }
public static GameObject CreateChildCollider(GameObject forObject, bool forPosition, Vector3?newPivotPos, Quaternion?newPivotRot) { string colliderName = ""; var colliders = forObject.GetComponents <Collider>(); if (colliders == null || colliders.Length == 0) { return(null); } #region Setting name of the child collider and disabling original colliders ColliderType lastType = ColliderType.None; foreach (Collider collider in colliders) { ColliderType currentType; //collider.enabled = false; if (collider is BoxCollider) { currentType = ColliderType.BoxCollider; colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType); if (lastType != ColliderType.None) { if (lastType != currentType) { colliderName = forObject.name + "_MixedColliders"; break; } else { colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType) + "s"; } } lastType = currentType; } else if (collider is CapsuleCollider) { currentType = ColliderType.CapsuleCollider; colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType); if (lastType != ColliderType.None) { if (lastType != currentType) { colliderName = forObject.name + "_MixedColliders"; break; } else { colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType) + "s"; } } lastType = currentType; } else if (collider is SphereCollider) { currentType = ColliderType.SphereCollider; colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType); if (lastType != ColliderType.None) { if (lastType != currentType) { colliderName = forObject.name + "_MixedColliders"; break; } else { colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType) + "s"; } } lastType = currentType; } else if (collider is MeshCollider) { currentType = ColliderType.MeshCollider; colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType); if (lastType != ColliderType.None) { if (lastType != currentType) { colliderName = forObject.name + "_MixedColliders"; break; } else { colliderName = forObject.name + "_" + Enum.GetName(typeof(ColliderType), currentType) + "s"; } } lastType = currentType; } } #endregion Setting name of the child collider and disabling original colliders colliderName = "**" + colliderName + "_DON'T DELETE**"; GameObject childCollider = UtilityServices.DuplicateGameObject(forObject, colliderName, false, false); foreach (Component component in childCollider.GetComponents <Component>()) { if (component is Transform || component is Collider) { continue; } DestroyImmediate(component); } foreach (Collider collider in forObject.GetComponents <Collider>()) { if (collider.enabled && forPosition) { collider.enabled = true; } else { collider.enabled = false; } } childCollider.transform.parent = forObject.transform; childCollider.AddComponent <CollRecognize>(); Collider coll = forObject.GetComponent <Collider>(); if (coll && coll is MeshCollider) { if (newPivotPos != null) { //childCollider.transform.position = (Vector3)newPivotPos; } else if (newPivotRot != null) { //childCollider.transform.rotation = (Quaternion)newPivotRot; } } return(childCollider); }