- Home /
How to scale 3D HUD with Camera Size?
I have a 3D HUD set up using some 3D models and an Orthographic Camera set to render as a regular 3D HUD should. The big problem here is whenever I change the screen size, the Camera's view scales to scale the view of the game and it leaves behind the 3D HUD causing it to appear off-screen/too far inward. Is there any way to move the 3D HUD to keep it in the upper-left of the Camera view regardless of the Camera View's size?
Answer by MD_Reptile · May 22, 2014 at 05:10 AM
The way I handled this, is by using a script to control button adjustments (to match screens of any size on android, but you could apply it here as well) and I would get the corners and areas of the screen (to position buttons based on where they are) like this:
bottomLeft = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
bottomRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 0));
topRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1));
bottomMiddle = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0));
And I would take a buttons position and relocate it based on where the camera was like this:
ThrustButton.transform.position = new Vector3((bottomLeft.x + 1), -1.25f, 5);
or:
PauseButton.transform.position = new Vector3((topRight.x - 1.5f), (topRight.y - 1.25f), 5);
and this way I could forget about what screen size were using, and just decide how far from the corners to place buttons...
and as well I would scale the buttons based on this, something like:
if(Screen.width >= 800)
{
fullSize = true;
//Debug.Log("FullSize scale");
buttonScale = 0.3f;
}
if(!fullSize && Screen.width >= 480)
{
midSize = true;
//Debug.Log("MidSize scale");
buttonScale = 0.275f;
}
but this probably won't work changing it at runtime, as I set these values from the get go. Good luck!
I'm not quite sure how to use the Camera variables. It has to be called from the main thread? Also, I'm using 3D models in the scene for the HUD, not buttons.
I think he's referring button scale as the the 3d Hud you have, you can make you'r own 3d HUD buttons that function like other buttons such as that which comes from the 2d GUI :) , also if your new to program$$anonymous$$g try to keep the adjustment code out of update and into where you change the screen.
Yes, I know. I am not trying to make a button. I'm trying to make a 3D counter for a certain objective item. All I need is to keep it in the top-left of the camera view regardless of the size.
doesn't matter it's just a variable name, it doesn't affect the functionality of the code. you could have ElpehantEarScale ins$$anonymous$$d of button-scale it won't matter.
think of it this way. if a 3D button moves ans stays to the top left, so will your counter :)
I figured it out. That is the answer, but I just didn't know how to call the Camera code right. I figured it all out and it works great! Thanks everyone!
Your answer
Follow this Question
Related Questions
Trying to use an orthographic camera in interior scenes 0 Answers
How do I implement orthographic elements into a perspective camera? 0 Answers
How Do I Build My Application To The Size Of The Camera 1 Answer
Set Orthographic Camera To Always Have Y Coordinate Start At Set Position 0 Answers
Align scene view camera to HUD camera? 0 Answers