I am trying to find the screenspace bounds of an object so that I can draw a box around it. I don’t think I can use a canvas or any other GUI method because I want the box to be occluded by other objects if the target object goes behind them.
I also want to be able to use this with a skinnedmeshrenderer so standard bounding boxes wont work. I am currently projecting the vertex array from the mesh into the camera space like this:
Camera cam = Camera.main;
while (i < vertices.Length){
vertices _= cam.WorldToScreenPoint(vertices*);*_
* i++;*
* }*
I then find the screenspace max and min points which I place in a Vector3 List screenMinMaxList then transform them back into world space (and add in an animation transform) like this:
for (i = 0; i < screenMinMaxList.Count; i++) {
_ Vector3 temp = screenMinMaxList*;
temp = cam.ScreenToWorldPoint (temp);
temp += trans.position;
screenMinMaxList = temp;
}*
I then draw a rectangle using the min and max X and Y values at a constant depth from the camera. The rectangle is way too small._