Monday, July 14, 2014

[Tutorial 2] Gomoku in Unity - Player control

4:05 PM Posted by Unknown 4 comments
Hi everyone,

Welcome to my second tutorial. In this session, we will explore how to make player control. However, we are not going make player vs computer yet, just player vs player in this tutorial because it can be easy to understand before we move to the third tutorial about making computer AI (Artificial Intelligence) and checking winner.

*All scripts are written in C# but Unity also support JavaScript and Boo, so if you don't know C# but JavaScript, that's fine because they are not so different.
* If anyone have a question, feel free to ask and I will try to answer as fast as possible.
*Anything in this tutorial can be wrong, so if anyone see it, you guys can feedback and I will correct it to improve the tutorial. Any help will be greatly appreciated.

OK, let's start the tutorial and hope you enjoyed this session.

1. Player Control

As a previous tutorial, we finally built a game board with size 16 x 16. Now, with player control, you will do the following step:
- Create a new GameObject and set their name to GameManager (or anything you like).
- This GameObject will manage the player turn and control.
- Add new script to this object and named it GameManager (or anything you like).
- Open the script in MonoDevelop.

To control the player turn like player 1, player 2 or Computer. Declare a new enum Type that define player turn state.








Then declare a variable playerTurn with PlayerTurn type that we just created. Morever, we need to replace the tile where player click on with a new sprite (X or O) so put 2 variables of type Sprite in the script (remember to set it public). 









Alright, so what we will initialize in our Start () function ?. To determine the who will go first, we can use the Random to roll the number from 0.0 -> 1.0, if number from [0.0, 0.5) then player 1 go else [0.5, 1.0] then player 2 go.









Next, we will detect when player click on the screen then the empty tile will be replaced by X or O tile based on player turn.















So when we clicks on screen, how do we know which object we are contacted. In the 2D environment like this, Physic2D.Raycast will do this job for you.

Physic2D.Raycast (Vector2 origin, Vector2 direction, float distance)

What is Raycast means ?. Well, it's like the function will draw a straight line from your position (origin) with direction and distance on your screen, then they will return a type RayCast2D (Information returned about an object detected by a raycast in 2D physics). Let's have a look at below image, you will know what I means.
















After we cast a ray, we can get the collider which is a GameObject. However, we only replace the empty tile, not the tile of player 1 or player 2, so how to do this ?. Unity provides us a tag for each object but you may think that how about the name, they are sometimes the same. Let's me explain this as my understanding:
- Tag: In your game, you have a lot of object like monster, zombies and you can then marked them as "Enemy". Moreover, Tag must be declared in Unity.
- Name: You have so many objects that attack player and you know it is Enemy, now you want to set their name to what they are look like.
So you can use Tag when you want to group a bunch of thing and Name when you want to specify them. Normally, it depends on your game.

In our game, we only have 3 types object: empty tile, player 1 tile (x Tile) and player 2 tile (o Tile). So name or tag?, do whatever you want.

Therefore, when we click on that tile, we need to check that If it is the empty tile, then replace it with the appropriate tile. After that, based on the player turn, we will set the Sprite of that tile and change their tag. 

2. Result:

You can run the script now and see the result. I also attach my script, click to download: GameManager.cs


















That's all for our second tutorial, just comment any questions you don't know about this tutorial, I will try to answer it. See you guys again in my next tutorial.

4 comments:

  1. not working for me not colliding the tiles or not function the mousedown

    ReplyDelete
    Replies
    1. Hi,

      Sorry for taking too long to reply your comment, I was getting away from this Blog and nearly forgot it :). This tutorial was posted 2 years ago and after that time I was busy with my job so I haven't done any further tutorial. Sorry about this, I guess the whole stuff are not worked because the new version may be Unity 5, at the time I made this tutorial it was may be version 4, so apparently the functions in Unity have been changed a little bit

      I moved to Unreal Engine 4 for a while. So I will make some new tutorials a few months later in case anyone still want to see this game in action, I will try to make it done.

      Cheers.

      Delete
  2. I enjoyed first tutorial, but the second one is not working for me either. I would like to see youtube tutorials of this game, maybe drag and drop functions with X and O just for another option and more stuff for begginers. Keep up the good work ;)

    ReplyDelete
    Replies
    1. Hi,

      Thanks for reading my tutorial. This tutorial was posted 2 years ago and after that time I was busy with my job so I haven't done any further tutorial. Sorry about this, I guess the whole stuff are not worked because the new version may be Unity 5, at the time I made this tutorial it was may be version 4, so apparently the functions in Unity have been changed a little bit

      I moved to Unreal Engine 4 for a while. So I will make some new tutorials a few months later in case anyone still want to see this game in action, I will try to make it done.

      Cheers.

      Delete