- Home /
Instantiating and deleting objects with triggers problem
I'm trying to create some kind of dust field, by now I'm just creating small balls for testing, between the bounds of a BoxCollider which I use as my dust field. When the box collider goes outside the collider of one of the dust balls, it triggers the OnTriggerExit of that ball and creates a new instance of the ball in a random position at the front and removes the current instance.
The problem I have is that I find some of the balls on the sides of the box (along the x axis) not colliding with the dust field's BoxCollider as soon as the object with the BoxCollider starts to change direction. Since it doesn't collide, it also doesn't call OnColliderExit, and I need to call for the creation of a new dust object and the deletion of the current one at that point. So if I watch on the editor, I can see ball objects leaking on the sides of the box.
I've tried to use some limit to the bounds of the box for the dust object placement (limitPerAxis variable in code) but only works well when using something like 0.5 or 0.6 (50% - 60%) of the area of the BoxCollider, but doing that only makes the dust balls to appear in bursts because of the space left.
UPDATE
I've changed the rotation of the player object which is the parent of the box, and now I can see the balls get placed outside of the box when I set the Y rotation of the parent object to something like 45, or anything in between 0 and 90, 90 and 180 and such, even when created on Start event, so they get the chance of never collide with it. I've been checking that all sizes that might be used are set to 1 just in case. I'll upload some pictures so I can show better what I mean.
Here are the balls created inside the box when the player Y rotation is set at 0:
And this is what happens when I set Y rotation to 45. Balls are located outside the box. It seems like they are rotated by TransformPoint call, but also, like the player transform size components where bigger than 1, which are all 1:
This is how I create the objects:
public class DustFieldCreator : MonoBehaviour {
// Use this for initialization
public GameObject DustObject;
public GameObject World;
public int Particles;
private BoxCollider boxCollider;
private const float limitPerAxis = 0.75f; // Ratio
void Start () {
boxCollider = GetComponent<BoxCollider>();
createField();
}
private void createField()
{
for (int i = 0; i < Particles; i++)
{
float xpos = Random.Range(((boxCollider.bounds.size.x / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.x / 2) * limitPerAxis);
float ypos = Random.Range(((boxCollider.bounds.size.y / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.y / 2) * limitPerAxis);
float zpos = Random.Range(((boxCollider.bounds.size.z / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.z / 2) * limitPerAxis);
createParticle(xpos, ypos, zpos);
}
}
private void createParticle(float x, float y, float z)
{
GameObject dustInstance = (GameObject)GameObject.Instantiate(DustObject);
dustInstance.transform.parent = World.transform;
Vector3 dustPosition = new Vector3(x, y, z);
dustPosition = transform.TransformPoint(dustPosition);
dustInstance.transform.localPosition = dustPosition;
DustBall ball = dustInstance.GetComponent<DustBall>();
ball.DustField = gameObject;
}
public void createParticleInFront()
{
float xpos = Random.Range(((boxCollider.bounds.size.x / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.x / 2) * limitPerAxis);
float ypos = Random.Range(((boxCollider.bounds.size.y / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.y / 2) * limitPerAxis);
float zpos = (boxCollider.bounds.size.z / 2) * limitPerAxis;
createParticle(xpos, ypos, zpos);
}
// Update is called once per frame
void Update () {
}
public void OnGUI()
{
}
}
The dust ball method for OnTriggerExit:
void OnTriggerExit(Collider collider)
{
if (collider.gameObject == DustField)
{
Debug.Log("Ball outside field");
dustCreator.createParticleInFront();
GameObject.Destroy(gameObject);
}
}
What is that I'm doing wrong?
Your answer
