Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 801
mtalka Avatar asked Mar 24 '14 09:03

mtalka


2 Answers

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!

like image 153
CliveCleaves Avatar answered Oct 22 '22 00:10

CliveCleaves


  1. Create 8 empty game objects.
  2. Make them children of the "cube" object to be tracked.
  3. Move them in Editor to be at the 8 corners of your tracked game object. Since they are children of the tracked object, their positions will be {+/- 0.5, +/- 0.5, +/- 0.5}.
  4. Name them memorably (i.e., "top-left-back corner").
  5. These 8 objects will move and scale with the tracked object, staying at its corners. Any time you want to know where a corner is, simply reference one or more of the 8 game objects by name and get the Vector3 for each of their transform.position.

Note: If you're doing step 5 repeatedly, you may want to avoid potentially costly GameObject.Find() calls (or similar) each time. Do them once and save the 8 game objects in a variable.

like image 32
Brad Knox Avatar answered Oct 22 '22 01:10

Brad Knox