Sign in to follow this  

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

This topic is 1111 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

This topic is 1111 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      628698
    • Total Posts
      2984275
  • Similar Content

    • By Mert Oguz
      well, i have started developing games last year, alone , I made a singleplayer 3d openworld rpg on unity you can look at it on googleplaystore ( kooru stone rpg ) whatever, this year, i wanted to make mmo, which gone really fine until I first try real hosting, I was working on "wamp" until then. The reason i am desperate now is that the way my game works.
      On my pc, using wamp mysql , with localhost as host for my game, i was testing my mmorpg with using andorid emulators, ofcourse no lag no issues no restrictions, beautiful dream... But then, I wanted to get real host from web, so, I rent a basic, cheaphest ever web host ( 10$ year ), and transferred my php files along with sql database. 
      So, I launched the game, still no issues, tried to handle 2-3 players by using my pc, phone, friend's phone...  
      After a while, ( after really short time (3-4mins)) host started not to respond, beacause those web hosting were not fit to handle mmos, i predicted that.
      now what i am explaining is that my game works like this and asking what way should i use to handle it :
      - Creates web request ( like : webhost.com/game/getplayerdata.php?ID=2 )
      -Reads request ( request result be like = "ID2-GoodGuyXx-23-123-4-123-43 )
      -Builds player using result string
      -does similar requests REEAALY FREQUENTLY  ( total requests of 8 - 12 times per seconds )
      With my current ultimate cheap web hosting, i can handle 2 players with low lag ( lol ) but, i want to handle around 20-100 players,
      just need a clear path, i have been struggling with google cloud sql and other vps server dedicated server options, i dont wanna pay much and get ripped off.
    • By Apollo Cabrera
      That planet is awesome!!! It's just slowly dissipating into space. Death by emmissions
      Love the green and yellow-orange juxtaposition (did I just say that?).
    • By Marc Vandenbosch
      D.O.T is an abstract arena shooter in which you fight for highscore with good or bad weapons.
      Follow me if you want more info:  https://twitter.com/bombjackm
    • By Apollo Cabrera
      Yasss!!! My first Unity3d game is on the App Store and Google Play.
      Download please! About 30 minutes to get through 5 missions.
      Let me know what you guys think.
      Thanks a bunch
      Bleeping Bots MacOsx Demo Cut.mp4
    • By TheZeph
      I've been doing some digging and come to the conclusion that I'm not really qualified to identify a game engine for my project alone so I came here asking for some advice.
      My game idea involves movement inside a sphere where you always have your feet on the ground (the "walls" of the sphere), basically an inverted planet. It's supposed to be multiplayer so it's not just you moving in the world. When you jump, gravity (or force etc) pulls your character towards where it jumped from and the character model should have its feet pointing that way.
      I'm really new to this and my own conclusion is that it messes with core functionality of Unreal Engine 4 which makes it really hard for me as a beginner to wrap my head around. It comes with practice but It's hard for me to come to a fair conclusion on what engine to use based on my knowledge. Therefore I'm asking for some guidance. I have most experience with the Unreal Engine.
      What engine would you recommend?
      Is the game idea with being inside a sphere difficult to do in general? Do you have any tips?
      I'm kinda lost at the moment so thank you for the help!

  • Popular Now