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(3. How to make a 2D Game - Unity 4.3 Tutorial - YouTube) but it didn’t work very well
i appreciate all kind of help!
I recommend reading [this][1] 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*;*
vertex1.x = vertex1.x;
vertex1.y = screenHeight;
vertex1.z = vertex1.z;
verts1 = vertex1;
}
for (int i=0;i<verts2.Length;i++)
{
Vector3 vertex2 = verts2*;*
vertex2.x = screenWidth;
vertex2.y = vertex2.y;
vertex2.z = vertex2.z;
verts2 = vertex2;
}
for (int i=0;i<verts3.Length;i++)
{
Vector3 vertex3 = verts3*;*
vertex3.x = vertex3.x;
vertex3.y = screenHeight;
vertex3.z = vertex3.z;
verts3 = vertex3;
}
for (int i=0;i<verts1.Length;i++)
{
Vector3 vertex4 = verts4*;*
vertex4.x = vertex4.x;
vertex4.y = screenHeight;
vertex4.z = vertex4.z;
verts4 = 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!
_*[1]: http://docs.unity3d.com/ScriptReference/Mesh-vertices.html*_