A good lecture that enabled me to customise a little.
I had an issue where i have rescaled my enemies as they were tiny and so the box colliders being added were based on the mesh render size. Not entirely helpful.
This meant i needed a script for each enemy that needed box colliders adjusting.
This also meant some of my enemies needed the collider center moving too.
I could sort this out playing with scale and sorting the enemy out but i chose to go code and do it at run time.
Note I am aware that i can do this both in the enemy script and only use one but for simplicity if you have multiple enemies i made them separate.
In the future this will get refactored into one script probably utilizing tags in unity as i have flying and ground units.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyWarrior : MonoBehaviour
{
// [Tooltip("FX Prefab on Player")] [SerializeField] GameObject enemyDeathFX;
// Use this for initialization
void Start ()
{
AddNonTriggerBoxCollider();
}
void AddNonTriggerBoxCollider()
{
if (gameObject.AddComponent<BoxCollider>() == null)
{
Collider boxCollider = gameObject.AddComponent<BoxCollider>();
boxCollider.isTrigger = false;
BoxCollider b = GetComponent<BoxCollider>();
if (b != null)
{
b.size = new Vector3(0.75f, 0.25f, 0.75f);
b.center = new Vector3(0f, 0.25f, 0f);
}
}
else
{
Collider boxCollider = GetComponent<BoxCollider>();
boxCollider.isTrigger = false;
BoxCollider b = GetComponent<BoxCollider>();
if (b != null)
{
b.size = new Vector3(0.75f, 0.25f, 0.75f);
b.center = new Vector3(0f, 0.25f, 0f);
}
}
}
// Update is called once per frame
void Update ()
{
}
void OnParticleCollision(GameObject other)
{
Destroy(gameObject);
}
}```