• How to set limit for player moving in 2D game
How to set limit for player moving in 2D game

Welcome to our Unity tutorial series.

In this tutorial, we will show you how to set limit for where player can go and how to look camera to player when it’s moving.

We will use our last project from previous tutorial for an example. You can check that tutorial out or download only the project from this link.

In this project, we already have one object (pacman) that we can move it by pressing keys, but it can move in an infinite space, now the question is how to make a room for our player. Means we want to keep our player to move only in a certain space.

 

Our idea is to create a room like this and let the player move inside it.

 

It looks like an empty object with Box Collider 2D, but it’s not simple like that. Because we can’t let an object move inside one collider of another object. It will be pushed out automatically.

To fix this issue, we need to create four objects instead, four objects and four colliders for four walls of the room.

That is our idea, now let’s do it.

 

Right click on Hierarchy window ->Create Empty -> Rename it to Room

Now we will create four child objects for four walls inside this Room object.

Right click on Room object ->Create Empty -> Rename it to Wall_top

Create another one and rename it to Wall_right

Select two child objects we just created, set Scale X of Transform is 100 (This scale number depends on how big the space you want to make).

Create a collider as a line to detect collision with player:

Click Add Component -> Physics 2D -> Edge Collider 2D

 

Now select the Wall_right object, this wall is situated on the right side and is vertical, so we have to set the position and rotation of it like this:

 

We only set the position of X offset to 50, because we still have one more wall on the left side. And we set the rotation with Z pivot to 90 in order to make it become vertical.

Select the Wall_top object and set its position of Y offset to 50.

 

 Now select both of them again in Hierarchy window and press Ctrl + D (on Windows) to duplicate them.

 

Everything in two objects was duplicated is the same as their originals but the name.

Change the name of Wall_top (1) object to Wall_bottom and the name of Wall_right (1) object to Wall_left.

Change the X position of Wall_left object and the Y position of Wall_bottom object to -50 to complete four walls of our room.

Now the room is set up, but it can’t keep our player (pacman)moving inside because our player can’t differentiate between cake and walls, so it…eats all the walls.

To help our player, we will show you how to use Tag in Unity.

We will add a tag to our cake and let our player know whether it colliding with a cake or with a wall.

First, click on any object in Hierarchy window -> Click on select Tag in Inspector window ->Add Tag…

 

 Click on the small plus sign on bottom of Tags to create a new Tag:

 

Set name for it is something like Cake.

 

Select our pizza object in Hierarchy window -> click on select Tag in Inspector window and choose the Cake tag we just created:

 

 

That’s all we need to do in Unity Editor, but we still have one more thing to do in Script Editor.

Open our MovingObject script from pacman object.

Add the if statement into OnCollisionEnter2Dmethod like this.

void OnCollisionEnter2D(Collision2D col)

{

//Remove the instance of the game object that this object is colliding with but only if it is a cake

if(col.gameObject.tag =="Cake")

{

        Destroy(col.gameObject);

        print("The cake was eaten by pacman...!");

}

}

 

This col.gameObject.tag will return the value of tag that the object is colliding with player is set to. Only if the return value is equal to “Cake” (The tag of pizza object), the code inside if statement will be executed.

Now save the script and come back to Unity. Enter play mode to test.

You can increase the speed of player to test quickly.

If you can’t move the pacman face to the outside of the room, that’s mean we’re success to create room for our player.

That’s all we need to do to keep our player moving inside a room. If you get any trouble, leave a comment for us so we can help you as soon as possible.

 

Thanks for reading and see you again in our next tutorial.