# Unity [SOLVED]Rotation of objects inside box collider bounds ends up out of the bounds

This topic is 1332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm playing a little bit with unity trying to create some kind of dust field which I've started by creating small sphere objects inside the bounds of a box collider. The problem that I have is that when I rotate the parent object of the one that carries such box collider, the spheres get placed outside of the bounds. Why is it? I've set all sizes to 1, so it doesn't scale when calling TransformPoint.

UPDATE:
Just found out, that the size property of the box collider bounds varies with rotation. I don't know exactly why, but I guess it recalculates the size of the box not taking into account that the box is rotated, so when rotated 45º degrees it says its bigger. I've been trying to retrieve the size value in Start and Awake events but its already changed from the original value I've set up in the editor. In the docs, http://docs.unity3d.com/ScriptReference/Bounds.html I only see two relevant properties related to the size that aren't related to the position, size and extents, but both change when rotated. What could I do to retrieve the original size I've set in the editor?

SOLUTION:
I was looking at the wrong property of the boxcollider. Its not boxcollider.bounds the one that gives me local space data, but boxcollider.size.

Here is the code I wrote for placing 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 = 1; // Ratio. This was used to resize the effective bounds for testing. Don't mind :D

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()
{

}
}


I've attached some screenshots to show you how the problem looks.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002326
×