terça-feira, 14 de junho de 2016

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

Sem comentários:

Enviar um comentário