Unity3D: How to determine the corners of a gameobject in order to position other gameobjects according to it?

mtalka picture mtalka · Mar 24, 2014 · Viewed 13.5k times · Source

My question is about if there is a way to know the coordinates of the corners of a gameobject. What I have is three Vuforia AR targets and a gameobject, a cube in this case.

What I need to achieve, is so that when I move the targets around, the corners of the cube would follow the targets, eg. the cube would be as wide as the space between the targets.

Right now how it does it, is that it checks the distance between the targets and sets the scale of the cube according to it. This results in the cube being expanded always from its set position, which makes the positioning of the targets awkward in real life.

Here's a couple of pictures showing what it does now, taken during execution:

Here is the code attached to the cube-object:

using UnityEngine;
using System.Collections;

public class show : MonoBehaviour {

float distancex;
float distancez;

// Use this for initialization
void Start () {
    renderer.enabled = false;
}

// Update is called once per frame
void Update () {
    if (GameObject.Find ("target1").renderer.enabled && GameObject.Find ("target2").renderer.enabled && 
        GameObject.Find ("target3").renderer.enabled && GameObject.Find ("target4").renderer.enabled) 
    {
        renderer.enabled = true;

    }

    distancex = Vector3.Distance ((GameObject.Find ("target1").transform.position), (GameObject.Find ("target2").transform.position));
    distancez = Vector3.Distance ((GameObject.Find ("target2").transform.position), (GameObject.Find ("target3").transform.position));


    Debug.Log (distancex);
    Debug.Log (distancez);

    transform.localScale = new Vector3((distancex), transform.localScale.y, transform.localScale.z);
    transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, (distancez));

    Debug.Log (transform.localScale);
}
}

How do I make the corners of the object follow the targets? I need the width of the side to be the width of the distance between the targets, is there any way to achieve this without using the scale?

Answer

CliveCleaves picture CliveCleaves · Mar 10, 2015

I know this is quite some time after you asked the question, but I came across this as I was looking to sort something similar out myself.

What I found I needed (and others may be helped) is to use Renderer.bounds

What it looks like in practice for me:

void Update () {
  rend = currentGameObject.GetComponent<Renderer>();
  Debug.Log(rend.bounds.max);
  Debug.Log(rend.bounds.min);
}

My object in this case was a quad at position 0,0,200 and a scale of 200,200,1. The output of rend.bounds.max is (100.0,100.0,200.0) the min was (-100.0,-100.0,200.0). This gives you the corner position for each corner (granted my example was in 2D space, but this should work for 3d as well).

To get it a little more specific for your example if you wanted the top right corner, you could get the XY of renderer.bounds.max, for the top left you would get the renderer.bounds.max.y and the renderer.bounds.min.x. Hope that helps!

Cheers!