Avançar para o conteúdo principal

PacMan 3D

In this post we will be making a simple and classic game, the Pacman, with Unity 3D.

Let's create a new a new project




Our game must start with a simple menu and that's what the first scene will be for. So let's add a button.


By adding a button Unity added a Canvas element. The canvas represents the view of the user interface (UI) so we must position the button within the canvas or it won't be visible.


Next step, create a script in C# to implement the function that will be associated with the event click of the button. Use the right button of your mouse in the project toolbar then choose create and C# Script.


Open the script with a double click and enter the following code.

public class Menu : MonoBehaviour {

    //function click for the button
    public void button_click()
    {
        SceneManager.LoadScene(1);
    }
}

The file created by Unity is a class derived from MonoBehaviour class that includes two functions (Start and Update) that, it this case we won't need.

Know we must select the canvas and add the new script to it. Next select the button and in the On Click event choose the canvas first and the function button_click next.

Let's save this scene, with the name menu and create a new one.

In this new scene we will be adding a plane, that will work as the floor of the level and a couple of cubes for the walls.

Each of this elements will have it material. For the floor we will be using a texture that needs to be added to the project. After that the floor material was configured as showed in the next image.

The Albedo field as the texture and the Metallic and Smoothness fields were tweaked in order to get the desired look. The tiling is 5 for X and for Y.

When the objects are inserted in the scene we must be careful with the position if each so the floor must be positioned in 0,0,0 and the others will use the floor as reference.

The new scene/level should look like this.


Let's create the player. We will use a sphere and some code that comes with Unity.

In the GameObject menu choose 3D Object and then Sphere.

The code is in the Unity Assets, so open the Assets menu pick Import Package and then Characters.

Go to Scripts in RollerBall, in Characters, in Standart Assets.
In that folder you will find two scripts. Drag the two to the player sphere.

Next we need to add a Rigidbody component to the sphere, like so.
A rigidbody is a physics component that will give the object the ability to comply with the law of physics, like gravity.

Right now the sphere should move with the input of the player. You may change the sphere behavior by changing some values in the Ball script, like the Move Power. The camera should be positioned so that the entire level is visible.

The player sphere must have a tag with the text Player. The tags allow the programmer to make code that will work in groups of objects at a time.

Who you're gonna call? Ghost busters! Yes, now we make the ghosts that will try to get the player across the level.

So the ghost has a capsule and a C# script that we will be adding later but the most important part is the Nav Mesh Agent component that will guide the Ghost through the maze. In order to get the collision done right we need to add a Character Controller component, without this component the collisions only are detected if the player is moving.


For the Ghost Script we add this code.

public class ghostScript : MonoBehaviour {

    public GameObject target;   //this is the player or a reference for him
    NavMeshAgent agent;         //this is a reference for the ghost navmeshagent component

    // Use this for initialization
    void Start () {
        agent = GetComponent<NavMeshAgent>();
        if (target == null)
            target = GameObject.FindGameObjectWithTag("Player");
    }
   
    // Update is called once per frame
    void Update () {
        //this is for updating the target location
        agent.destination = target.transform.position;
    }

    //function to detect when the ghost gets the player
    public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
            SceneManager.LoadScene("menu");
    }
}

The Nav Mesh Agent only works correctly if the floor plane is marked as static and if we go to Navigation toolbar and bake the pathfinding information necessary for the ghost.





The blue areas are walk-able.
Right now the ghost will follow the player, were ever he goes and when he collides with him the game will bring the menu scene back. The project must have defined the scenes that compose the game so let's go to File - Build Settings and add the scenes by dragging them.


It's just missing the next level point. For that let's add a Empty Game Object and add a box collider but with the Is Trigger property on like so.



And the code.
public class nextLevelScript : MonoBehaviour {

    public void OnTriggerEnter(Collider collider)
    {
        if (collider.tag == "Player")
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}

This tutorial is available on Youtube

The project can be downloaded from GitHub

Comentários

Mensagens populares deste blogue

New Unity 3D Project

Today I will present a new project that I started. From the post about the car I am building a game with cars, or transportation. The idea is very simple: the player starts with a car and a mission, when he is done with the mission he gets some cash that can be spent buying a new vehicle. Here are some pics: - the car in Unity - the car in Blender - working in the texture - looks great - a wheel - the texture in Gimp - back in Unity testing different materials  like water - and code

Let's make a car in Unity 3D

In this post we will make a simple car in Unity 3D. The Unity 3D physics engine is used in order to give the car a real behavior. This are the steps: [1] - Create a new Project

Single Page App with C# WPF/XAML

 In this post we are going to create a single page app. The app will have multiple pages that get rendered in the main window. We will be using Visual Studio, C#, WPF and XAML. Let's start by creating a new project in Visual Studio of this type: Next, in the MainWindow, we define the interface structure. On the left side we place a menu and on the right side a DockPanel with a Frame in it. The Frame is the element that is used to render de pages content. Now let's add the new pages. In this example I will add two pages. Click in the Solution Explorer with the mouse right button, then choose Add and Page. The project looks like this. The app content goes on the recently create pages. Because this is just an example I will just change the background color and add a small text. Page1 Page2 Finally the code. Back to the MainWindow we need to create the click events on the menu items. So, in the MenuItem line add the click event and pick New Event Handler. If that option doesn't...