- Home /
how to automatically set borders according to the screen size?
hello guys! i have a question, i want to make a game, in the gameplay pretty similiar to binding of isaac if anyone here knows it, i want to set the borders to each level so that they will fit the screen perfectaly, and start just at the borders of the camera. i tried to use a script brackeys used(https://www.youtube.com/watch?v=rlLMwNI53Oo) but it didn't work very well :( i appreciate all kind of help!
There are a few ways to do it but first of all, do you need (like Binding of Isaac) multiple borders in one scene or are you just going to use multiple scenes? If you are using multiple scene's you don't have to mess around with mesh vertices too much so it's fairly straight forward. So if you can get back to me on this I can help you more!
I will need 4 borders, just as in binding of isaac, it will be a top-down game so... anyway thank you very much for your help! also can you maybe explain what mesh verices as you tell me how to make those, so I can learn for next time? I'm pretty new to the engine, thanks a lot!
Answer by Calum1015 · Jun 18, 2015 at 12:03 AM
I recommend reading this to help you get started on vertices. Vertices are sort of like pivotal points in mesh's. They define how big a mesh is, what the angles in the mesh are, etc. Fortunately, with Unity you can edit the position of these vertices. This means that we are able to change the vertices of a mesh to fit like a border on the screen, by using Screen.height, and Screen.width.
Now, for the mechanics of the boundaries. Assuming you want it to be like binding of isaac, when you pass through a door your position is changed and a new border appears. That means we must be able to call a function to do this, so instead of putting the boundary code in the update function, we will put it in the start function, and a new function called "UpdateBoundaries". Then you will want to put some code into those functions. You will want to access the screen width and height first by storing it in a variable. Such as this; public class example : Monobehavior { float screenHeight; float screenWidth void Start() { screenHeight = Screen.height; screenWidth = Screen.width; } }
Now that you have stored the screen height and width in a variable that is accessible throught the script you can access the mesh vertices.
public class example : Monobehavior
{
public Vector3[] newVertices1;
public Vector3[] newVertices2;
public Vector3[] newVertices3;
public Vector3[] newVertices4;
public Mesh boundary1;
public Mesh boundary2;
public Mesh boundary3;
public Mesh boundary4;
float screenHeight;
float screenWidth;
void Start()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
boundary1 = boundary1.GetComponent<MeshFilter>();
boundary2 = boundary2.GetComponent<MeshFilter>();
boundary3 = boundary3.GetComponent<MeshFilter>();
boundary4 = boundary4.GetComponent<MeshFilter>();
Vector3[] newVertices1 = boundary1.vertices;
Vector3[] newVertices2 = boundary2.vertices;
Vector3[] newVertices3 = boundary3.vertices;
Vector3[] newVertices4 = boundary4.vertices;
}
}
Now that we got our vertices we can change them. This shouldn't be hard now that we have finished getting access to them. We will now store the vertices in yet another variable to access their coordinates (And change them).` public class example : Monobehavior { Vector3[] newVertices1; Vector3[] newVertices2; Vector3[] newVertices3; Vector3[] newVertices4; Mesh boundary1; Mesh boundary2; Mesh boundary3; Mesh boundary4; float screenHeight; float screenWidth;
void Start()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
boundary1 = boundary1.GetComponent<MeshFilter>();
boundary2 = boundary2.GetComponent<MeshFilter>();
boundary3 = boundary3.GetComponent<MeshFilter>();
boundary4 = boundary4.GetComponent<MeshFilter>();
Vector3[] newVertices1 = boundary1.vertices;
Vector3[] newVertices2 = boundary2.vertices;
Vector3[] newVertices3 = boundary3.vertices;
Vector3[] newVertices4 = boundary4.vertices;
ChangeBoundaries();
}
void ChangeBoundaries()
{
Vector3 verts1 = new Vector3[newVertices1.Length];
Vector3 verts2 = new Vector3[newVertices2.Length];
Vector3 verts3 = new Vector3[newVertices3.Length];
Vector3 verts4 = new Vector3[newVertices4.Length];
for (int i=0;i<verts1.Length;i++)
{
Vector3 vertex1 = verts1[i];
vertex1.x = vertex1.x;
vertex1.y = screenHeight;
vertex1.z = vertex1.z;
verts1[i] = vertex1;
}
for (int i=0;i<verts2.Length;i++)
{
Vector3 vertex2 = verts2[i];
vertex2.x = screenWidth;
vertex2.y = vertex2.y;
vertex2.z = vertex2.z;
verts2[i] = vertex2;
}
for (int i=0;i<verts3.Length;i++)
{
Vector3 vertex3 = verts3[i];
vertex3.x = vertex3.x;
vertex3.y = screenHeight;
vertex3.z = vertex3.z;
verts3[i] = vertex3;
}
for (int i=0;i<verts1.Length;i++)
{
Vector3 vertex4 = verts4[i];
vertex4.x = vertex4.x;
vertex4.y = screenHeight;
vertex4.z = vertex4.z;
verts4[i] = vertex4;
}
boundary1.vertices = verts1;
boundary2.vertices = verts2;
boundary3.vertices = verts3;
boundary4.vertices = verts4;
boundary1.RecalculateNormals();
boundary1.RecalculateBounds();
boundary2.RecalculateNormals();
boundary2.RecalculateBounds();
boundary3.RecalculateNormals();
boundary3.RecalculateBounds();
boundary4.RecalculateNormals();
boundary4.RecalculateBounds();
}
}
By the way this script is untested so I don't know if it will work 100% but I hope that it does! If you need any more help just leave a comment! I would have left this as a comment but apparently it's too big by -1507 characters -_-. Good Luck with this!
thank you very very much! i'm not quite sure what is the definition of the word "mesh", tried to translate it to my mother tongue language but it still doesn't seem to make a lot of sense, if you could explain it in words or a picture or anything like that it'd be great, as i learn new stuff i want to fully understand them, i kinda understood it all but by understand what mesh is would help me a lot
Absolutely! A mesh is a series of vertices(I talked about them in the answer) that create an object. When you create an object in a 3D modeling software, this is called a mesh. Inside of Unity, when you create a cube, that is a mesh. Essentially it is just an object definition, meaning it holds the object data such as where to put "walls" or edges on your model. I hope this helps you understand a mesh. For more info on mesh's you should check out this page on the Unity Scripting and API. Hope this helped!
Edit - A mesh also holds triangle data which make of the wall or side of a mesh.
thank you for the explanation, i understand now, but how do meshes work on 2d if so?