c# - Unity3D: How to determine the corners of a gameobject in order to position other gameobjects according to it? -
my question if there way know coordinates of corners of gameobject. have 3 vuforia ar targets , gameobject, cube in case.
what need achieve, when move targets around, corners of cube follow targets, eg. cube wide space between targets.
right how it, checks distance between targets , sets scale of cube according it. results in cube being expanded set position, makes positioning of targets awkward in real life.
here's couple of pictures showing now, taken during execution:
here code attached cube-object:
using unityengine; using system.collections; public class show : monobehaviour { float distancex; float distancez; // use initialization void start () { renderer.enabled = false; } // update 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 make corners of object follow targets? need width of side width of distance between targets, there way achieve without using scale?
i know quite time after asked question, came across looking sort similar out myself.
what found needed (and others may helped) use renderer.bounds
what looks in practice me:
void update () { rend = currentgameobject.getcomponent<renderer>(); debug.log(rend.bounds.max); debug.log(rend.bounds.min); }
my object in case quad @ position 0,0,200 , scale of 200,200,1. output of rend.bounds.max (100.0,100.0,200.0)
min (-100.0,-100.0,200.0)
. gives corner position each corner (granted example in 2d space, should work 3d well).
to little more specific example if wanted top right corner, xy of renderer.bounds.max
, top left renderer.bounds.max.y
, renderer.bounds.min.x
. hope helps!
cheers!
Comments
Post a Comment