- Home /
Make multiple objects within the view of camera
The camera mode I am using is Orthographic view. I have two object to control and I wanted to keep them at cameras view without adjusting the cameras size (its actually okay if one or both objects becomes out of view) and the camera can move in one direction only that is upward or along the y-axis without going back downward.
So I think is the maximum distance between the two objects where we can get a full view of the objects other than that if one or both of the object touches the edge of camera it'll be game over.
Additional Info: The line between two objects is not mimicking a splitscreen but rather its more like an obstacle.
Your question is not clear. Do you want to keep the objects in view by adjusting the objects positions or do you want to change the camera parameters (position / size) to keep the two (or more) objects in view regardless of their positions?
edit i just realised you said you don't want to change the size so i guess you want to clamp the objects positions within the camera view?
I wanted to make the camera follow the 2 objects move and camera can move in y axis only.
So you do not want to move the objects? Since you mention changing how far the camera is, this is perspective camera ya?
Answer by Bunny83 · Oct 17, 2018 at 10:12 AM
Your question really was lacking important information. Please next time you ask a question be more specific and clear about what you actually want.
With the new requirements it's actually super simple. The optimal camera position to have both objects in view would be the center between the two objects. Since we only want to move the camera on y we can just take the arithmetic mean between the two y values and get the optimal y position for the camera. Of course since the camera should only move upwards we only adjust the camera position when the optimal y is greater than the current y position of the camera.
public Transform O1;
public Transform O2;
void LateUpdate()
{
float optimalY = (O1.position.y + O2.position.y) * 0.5f;
var pos = transform.position;
if (optimalY > pos.y)
pos-y = optimalY;
transform.position = pos;
}
This of course need to be attached to the camera and O1 / O2 should reference the two objects you're talking about.
If that's not what you want you may just want to adjust the camera if one of the two objects is about leaving the upper edge of the camera. So the top most object is responsible for "dragging" the camera upwards. In this scenario it's possible that both objects are actually very close to the upper edge. Also the top object will be most the time at the top edge. This is usually not what you want. It's also slightly more complicated.
This works. Sorry if the question was quite not clear but I did edited it and filled up important things.
Answer by MonoFlauta · Oct 16, 2018 at 04:10 PM
As you said, you can't see more than that without adjusting the camera size. Other option you can do, is using two cameras when they move too far. One to focus one target while the other one focus the other target. You can achieve this by placing to cameras, when both can seen in the same, you use only one and set unactive the other one. When they go to far, you turn on the other camera to make the split camera effect. In order to do this, you can use the Normalized Viewport Rectangle (more here: https://docs.unity3d.com/Manual/class-Camera.html) And you just switch from one to the other
Actually I am not making a split screen but rather the line between the two object is an obstacle
Then without the adjustment of the width and/or height of the camera it will not be possible to show both. At least not in 2D with an ortographic camera. Sorry about that
Yes its okay because when the two objects become out of view then it means game over so is more like also of a barrier/obstacle if I call it.