Search Unity

Finding the bottom of a transform/gameobject

Discussion in 'Scripting' started by RingOfStorms, Feb 8, 2012.

  1. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    Hey guys I am wondering if there is a way to find the height per say of an object then instantiate at the bottom of it. Atm, when I instantiate a particle system to gameobject.transform.position (the cube int he screen) - it is directly in the middle. Is there a way in code I can find the bottom w/o putting in a manual -4 or something so that it will work on any sized shape.



    So I wanna find the height and just subtract half of it and instantiate at the bottom of the cube. This is what I am using currently to find and do all that :

    Code (csharp):
    1.  
    2. Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
    3.         RaycastHit hit;
    4.         if(Physics.Raycast(ray/*Starting Point*/, out hit/*Direction of Ray*/, Mathf.Infinity/*Distance of ray*/)) {
    5.             if(hit.collider.tag == "Player1" | hit.collider.tag == "Player2" | hit.collider.tag == "Player3" | hit.collider.tag == "Player4" | hit.collider.tag == "Player5" | hit.collider.tag == "Player6" | hit.collider.tag == "Enemy") {
    6.                 objCached = hit.collider.gameObject;
    7.             }else{
    8.                 objCached = null;
    9.             }
    10.             if(objCached != cachedObj) {
    11.                 cachedObj = objCached;
    12.             }
    13.         }
    14.        
    15.         if(cachedObj) {
    16.             if(!PSwaitingClone) {
    17.                 PSwaitingClone = Instantiate(PSwaiting, cachedObj.transform.position, Quaternion.identity) as GameObject;
    18.             }else{
    19.                 PSwaitingClone.transform.position = cachedObj.transform.position;
    20.             }
    21.         }else{
    22.             Destroy(PSwaitingClone);
    23.         }
     
  2. vdek

    vdek

    Joined:
    Sep 2, 2011
    Posts:
    368
  3. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
    Yep, you want to find the Mesh bound extent on the y axis, and translate your effect downwards by that amount.
     
  4. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    Think you could give a slight exemplar on how to call the y value and go down to the bottom of the shape being selected
     
  5. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    Yea I still need some help on this, I've tried the mesh.bounds, renderer.bounds and still can't figure out how to do this. Any help would be appreciated.
     
  6. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Show what you tried, its disrespectful to assume others will guess
     
  7. tpainton

    tpainton

    Joined:
    Jun 4, 2014
    Posts:
    16
    It's obvious what he wants... I'd show him, but I have the same question and now, due to extremely poor intuition, we have no answer. And, no, I'm not rewording the obvious for you.
     
  8. Hunerstrike

    Hunerstrike

    Joined:
    Jan 15, 2020
    Posts:
    1
    No clue how to work with the Mesh Vertices, im assuming you'd have to use a sorting algorithm for the vertices, ill just utilize a physics raycast somehow for simplicity ig...