- Home /
Center an object in Ortographic view
Hi, I have a complicated problem to solve: I have a rectangular GameObject (a wall) that have to be centered inside an ortographic camera. It has to be zoomed in as much as possible but at the same time i need that he has a fixed padding from the sides of the screen (for example, from the wall at the edge of the screen there must always be a minimum of 60 px). The width of the wall is variable and therefore the size parameter must be recalculated each time after the wall resizing. I found this code to resize the wall height in order to fit the entire screen:
Camera.main.orthographicSize = target.renderer.bounds.extents.y;
I tried to modify the code as follows but did not get a proper solution:
public void changeOrthoSize () {
GameObject wall = GameObject.FindGameObjectWithTag("Wall");
float orthoSize;
float rulerPadding = 60;
if (wall.renderer.bounds.extents.x > wall.renderer.bounds.extents.y){
// divide by 2 the x size to catch the y size
if (wall.renderer.bounds.extents.x * 0.6f >= wall.renderer.bounds.extents.y){
orthoSize = wall.renderer.bounds.extents.x * 0.6f + rulerPadding * 0.5f;
} else {
orthoSize = wall.renderer.bounds.extents.y + rulerPadding;
}
} else {
orthoSize = wall.renderer.bounds.extents.y + rulerPadding;
}
Camera.main.orthographicSize = orthoSize;
}
Anyone have any suggestions for this problem? Thanks in advance
Answer by Cherno · Nov 19, 2013 at 11:10 PM
You could try the following:
Cast a ray from the center of the object towards your camera (make it give you screen coordinates) and use this coordinate to center in on the screen.
Find the corners of the wall facing the camera, and cast a ray from them towards the camera. Change the camera's FoV depending on wether the four corner coordinates are on the screen (you could also add/substract your padding pixels to each X and Y value).
Could you post a code example? In your explanation you talk about "FOV" but in orthographic camera i need to change the "size" parameters and it's not clear for me how to calculate it, thanks
Ok, if is possible can you post some code example? Thanks for your time
I did some testing and got it to work. Note that you have to find out yourself how to get and update the correctVector3 of the WallCorner1 (should be the wall's upper left corner).
You could probably calculate the Vector3 of the corner based on the wall's starting x dimension and the current x scale.
I added the +5 and -5 to give some leeway when updating, otherwise the orthographicSize will jitter all the time because of constant Updating. You could probably make this smaller.
var PlaneObject : GameObject;
var HorizontalPadding : int;
var VerticalPadding : int;
var WallCorner1 : Vector3;
var WallCorner1ScreenPoint : Vector3;
function Update()
{
PlaneObject.transform.position = Camera.main.camera.transform.position + Camera.main.camera.transform.forward;
WallCorner1ScreenPoint = Camera.main.camera.WorldToScreenPoint(WallCorner1);
if(WallCorner1ScreenPoint.x < 0 + HorizontalPadding - 5)
{
Camera.main.camera.orthographicSize += 0.1;
}
if(WallCorner1ScreenPoint.x > 0 + HorizontalPadding + 5)
{
Camera.main.camera.orthographicSize -= 0.1;
}
if(WallCorner1ScreenPoint.y < 0 + VerticalPadding - 5)
{
Camera.main.camera.orthographicSize += 0.1;
}
if(WallCorner1ScreenPoint.y > 0 + VerticalPadding + 5)
{
Camera.main.camera.orthographicSize -= 0.1;
}
}
Your answer
Follow this Question
Related Questions
Orthographic camera cuts off half of screen in Web Player - resolution error? 1 Answer
Finding the right ortho size to fill the screen with an object 1 Answer
How to make WorldToScreenPoint work with an orthographic camera? 1 Answer
How to make WorldToScreenPoint work with an orthographic camera? 0 Answers