Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem creating dynamically objects

Discussion in 'Scripting' started by ericmartinez, May 3, 2015.

  1. ericmartinez

    ericmartinez

    Joined:
    Feb 26, 2014
    Posts:
    9
    Hey there!

    I created a thread a time ago about how to make an object spawns next to another one. This it is the thread :
    http://forum.unity3d.com/threads/so...g-one-next-to-each-other.321018/#post-2082277

    The problem now is depending on the object spawned it can appears over the terraing or right in the middle (Y axis). Since every object has a different height/width I can't give them an exact Y-axis value.

    This code works perfectly, but I'm giving its position to the second object. Since both do not have the same sizes the second object is getting 0.5 for Y-axis, the position of the main object (a simply cube for now).

    Code (csharp):
    1.  
    2. secondObject.transform.position = mainObject.transform.position + mainObjecttransform.rotation * Vector3.right * - 1;
    3.             secondObject.transform.rotation = mainObjecttransform.rotation;
    4.  
    I tried this and visually looks nice for larger objects, but the smallers ones stay floating.

    Code (csharp):
    1.  
    2.             Vector3 pos = mainObject.transform.position + .mainObject.transform.rotation * Vector3.right * -1;
    3.             pos.y = secondObject.GetComponent<BoxCollider>().bounds.size.y;
    4.             secondObject.transform.position = pos;
    All these objects are models created in Blender, then imported to Unity.

    TL;DR Is there anyway to spawn an object with 0.5 Y-axis (over the terrain) no matter the size and the pivot point of the model? I know the Y-axis is calculated over the pivot point and in some cases the pivot point in the model is in the middle, or sometimes is at the top of the model (should I fix that in blender?).

    Thanks in advance.
     
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    An easy solution would be to simply create an empty gameobject and place your model inside of this.

    This way the pivot point is the parent gameobject and you can position anything as you see fit (allowing things to float if needed aswell.
     
  3. ericmartinez

    ericmartinez

    Joined:
    Feb 26, 2014
    Posts:
    9
    Thanks for answering. But how can I do it? All these objects are prefabs and I instatiate them dynamically. I can't add the game object to the prefab. The empty gameobject goes directly into the hierarchy, while the prefab is not in it.
     
  4. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    If it has a collider you can use collider.bounds.extents.y or something to know the distance from the center to the bottom of the gameObject
     
  5. ericmartinez

    ericmartinez

    Joined:
    Feb 26, 2014
    Posts:
    9
    Thanks for your answers guys, but I can't find a real solution. All my objects have completely different shapes (sizes and scales) and most of them have different pieces, so getting the Y from the collider it's not that easy, say for example I have a bird which has 4 parts : body (head and body), tail, wings (from the body and over the head) and the claws (which are below the body) so there's no a standar collider for that object. By surrounding the object (the 4 pieces) within a cube I can have a more exact collider, sizes and all that stuff.

    So I will have to find a way to standarize them without making one too big and the other one too small. This must be done in Blender I guess.

    For now I "solved" it by adding a cube (without mesh) around the object so the scale and the positioning are easier, but there are still a lot of issues. For example one of the objects it's a little above the terraing, the second one is "exactly" over the terrain, the third one is a little below of it (if I add 0.1f to the Y-axis it works), and the last one seems fine. So there are too many variables, I'll have to find some way to fix this.


    I'm still learning this, and I won't give up.

    I'll comeback if I find a final solution to this.

    Thanks ;)