Languages & Coding > Unity

Fit object to bounds

(1/1)

GfK:
Bit of a long shot as I don't see much Unity activity here, but here goes...

In my game I want to have it so when I target an object, an image of the target is rendered to the HUD.

I'll do this by having a camera way below the playfield, and place a copy of the targeted object in front of it.  For this to work correctly I'll have to scale the object and it's children down uniformally so it's fully contained within a 5x5x5 volume.

I can solve this mathematically but I am sure I've previously come across a quicker method, though I can't find it now.  Something like Mesh.FitToBounds()?

Using UnityPro 5.6.5.

Any suggestions?

Steve Elliott:
ENAY is probably the man to answer your question.  Hopefully he'll jump in at some point.

ENAY:
Hi Dave, do you have it solved, except you're trying to get the scale to match up correctly to a certain size?

Not entirely sure what you're trying to achieve.

Have you seen this post?

https://answers.unity.com/questions/1268885/rescaling-mesh-or-gameobject-to-fit-in-gui-rect.html

GfK:
Well I've solved it in my head - haven't got around to coding it yet.  I just need to find the longest dimension and divide 5 by that to get the scale factor, so the entire models then fits within a 5x5x5 cube.

I can then render it to a texture and apply it to the HUD.

The idea is that the scaled model will fill the HUD/render texture as much as possible, whether the target is big or small

Probably no big deal doing it this way, I just thought I'd seen something along the lines of GameObject.FitToBounds(5,5,5) and didn't want to waste time coding my own if there was something in there already.

ENAY:
RenderToTexture in Unity is awesome. Good luck with your project Dave.

Not really related to your project but if you're fannying about with viewable and non viewable objects in and outside a hub. I highly recommend reading up on stencil shaders, for Z ordering, and hiding and showing objects in various camera was really handy.

Will only take up 10 mins your time. Just watch this video. I did and it really helped. :)

&t=102s

Navigation

[0] Message Index

Go to full version