Jump to content
  • Advertisement
Sign in to follow this  
Adyrhan

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

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

If you intended to correct an error in the post then please contact us.

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.
 

Edited by Adyrhan

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!