- Home /
Getting Bounding box from a collider with children
hi there my friends , ok here is my problem: i have an empty game object and it have a lot of child objects (its a room) i want when i hover my mouse at it , it gets highlighted by the bounding box of the room , the problem is the empty game object does not have a renderer or anything else , only a collider its like this:
MeeteingRoom (empty game object that have only a box collider)
otherobjects (the models themselfs)
.....
.....
.....
the code for mouse hover is done and working fine , but how i could highlight the whole object is still not working , i was thinking of using mesh.bounds but the empty game object dont have a mesh (it must not have mesh).
on the other hand , i tried the following code:
var roomObjects : GameObject;
var highlightmat : Material;
var selectedObject : GameObject;
function Update(){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 500)) {
Debug.DrawLine (ray.origin, hit.point);
if(hit.collider.tag == ("Rooms")){
Debug.Log(hit.collider.gameObject);
selectedObject = hit.transform.gameObject;
//isHighlighted();
}else{
Debug.Log("Notting!!");
selectedObject = null;
//NotHighlighted();
}
}
}
function isHighlighted(){
}
function NotHighlighted(){
}
function OnPostRender()
{
if( selectedObject == null )
return;
var go = selectedObject;
highlightmat.SetPass( 0 );
var meshes = go.GetComponentsInChildren(MeshFilter);
for( var m : MeshFilter in meshes )
{
Graphics.DrawMeshNow( m.sharedMesh, m.transform.position, m.transform.rotation );
}
}
it works great but the scale is wrong bcz the importer scales the imported objects from 1.0 to 2.54 , how can i take the scale and add it to the above script?
thanks for any help i get.
Colliders have bounds too - is that not what you need?
i tried using it but didnt get it right , i have read the docs about bounds , but still i cant get it right , can u post just an example on how to use it probably.
thx for ur time
Answer by ScroodgeM · Jul 25, 2012 at 09:15 AM
solution 1
make a raycast from mouse, take returned by hit collider, then an object of this collider, then root of this object. if this object is your room - make a highlight recursively to childssolution 2
make a script with OnMouseOver method for each child and on mouse over event turn on highlightsolution 3
create a one big collider that covers whole room and attach it ro empty gameobject. then use simple OnMOuseOver from previous solutionthanks my good sir for ur help , but i have the script for raycasting from a mouse working fine , wt i was asking is how i can make a proper highlighting graphics for the highlighted objects as whole while the collider is only just a big bounding box that have only collider in it.
still thanks a lot for ur time and ur solutions.
if your question is how to access other gameobjects:
you can make a object with a collider as parent for all objects that should be highlighted, and then just recursively take them all (for each (Transform child in transform) {})
if your question is how to change graphic:
you can use a different material with another shader like self-illu$$anonymous$$ated ins$$anonymous$$d of diffuse, and change for all objects material to highlighted one
you can use a different shader and change for all objects shader
you can use a shader with highlighted mode, for example - self-illu$$anonymous$$ated shader that have illu$$anonymous$$ation color. changing this color from black to some color will cause object to be highlighted
i was looking for the for accessing other gameobjects , i tried it and it works , thx a lot for ur help.