Hi, I’d like my camera to always stay/show within the borders of a background (made of multiple game objects).
The basic element of my game is a Unity Sprite of 64 x 64 pixels.
That said. I know the position of the bottom left element (that is a gameobject with 64x64 sprite) and the top right (same type of game Object is a 64x64 sprite).
Example:
The position of these elements may change depending on the scene level. But they will stay fixed for all the scene, so I need to adjust this just after each level loading.
How can I clamp the camera between these 2 gameobjects (to avoid showing outside elements in the view)?
Thank you very much in advance.
I am REALLY NOT sure if this is gonna work…
using...blah blah blah
public class Clamp : MonoBehaviour
{
public GameObject bound_one; //assign in in the editor
public GameObject bound_two; //assign in in the editor
public Camera cam; // --
private float BOposX;
private float BOposY;
private float BTposX;
private float BTposY;
void Update()
{
float xPos = Mathf.Clamp(xPos, BOposX, BTposX);
float yPos = Mathf.Clamp(xPos, BOposY, BTposY);
cam.transform.localPosition = new Vector3(xPos, yPos, 0);
//if the localPosition line above does not work, try:
cam.transform.position = new Vector3(cam.transform.position.x + xPos, cam.transform.position.y + yPos, 0);
}
}
Sorry if this is totally wrong. Just place the Boundary GameObjects (you can create empty) in the needed places and it could somehow work. 