March 26, 2019, 07:20:46 AM

Author Topic: Fit object to bounds  (Read 683 times)

GfK

• Jr. Member
• Posts: 5
Fit object to bounds
« on: January 23, 2018, 08:52:52 PM »
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

• Hero Member
• Posts: 1571
Re: Fit object to bounds
« Reply #1 on: January 24, 2018, 06:25:15 PM »
ENAY is probably the man to answer your question.  Hopefully he'll jump in at some point.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)

MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.

ENAY

• Full Member
• Posts: 237
Re: Fit object to bounds
« Reply #2 on: January 29, 2018, 03:34:32 AM »
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?

GfK

• Jr. Member
• Posts: 5
Re: Fit object to bounds
« Reply #3 on: January 29, 2018, 06:45:14 PM »
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

• Full Member
• Posts: 237
Re: Fit object to bounds
« Reply #4 on: January 30, 2018, 01:25:23 AM »
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