---
Part 1: Grid movement
Part 2: Camera control
Part 3: Lighting
Part 4: NPC AI
Setting up the project
First, let's decide our cell size in the grid:
32 x 32 pixels - done.
Then start your new project normally (in 2D mode), let's call it RPGTutorial. Now I made a really simple 32 x 32 pixel square character for this purpose and imported it to Unity:
![]() |
The character |
![]() |
Drag the character to Assets in Unity project |
Then use the inspector (click the player sprite) and change the pixels to units to 32 and hit apply:
Then, for prototyping purposes I made a grid background (you will probably make the level with a tile based editor but for this purpose, I just quickly whipped up a single image and imported it to Unity):
![]() |
Background |
Also change the pixels to units to 32 for this asset, then drag it to the scene. Also drag the player sprite on top of the grid like so:
![]() |
Scene view |
Change the transform X and Y in the inspector to 0 (zero) for the grid to center it. For the player change the X and Y to -3.5 to put it in the corner.
Scripting
Create a new C# script in the assets and name it PlayerController. Edit the file like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { private Vector2 pos; private bool moving = false; void Start () { // First store our current position when the // script is initialized. pos = transform.position; } void Update () { CheckInput(); if(moving) { // pos is changed when there's input from the player transform.position = pos; moving = false; } } private void CheckInput() { // WASD control // We add the direction to our position, // this moves the character 1 unit (32 pixels) if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) { pos += Vector2.right; moving = true; } // For left, we have to subtract the direction else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) { pos -= Vector2.right; moving = true; } else if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) { pos += Vector2.up; moving = true; } // Same as for the left, subtraction for down else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) { pos -= Vector2.up; moving = true; } } } |
Save the script and drag it from the assets to the player object in the Hierarchy view. Hit play and test the movement with WASD keys.
---
Part 1: Grid movement
Part 2: Camera control
Part 3: Lighting
Part 4: NPC AI
Code highlighted with hilite.me
hi, i'm foreigne. so english level is very low. I have a problem. I passed my character's grid move, but my character passed collider2d tiles. how to know move tiles and non move tiles.
ReplyDeleteHi! Thanks... I have been writing a tutorial for collision but it is not finished yet sadly.
DeleteAnyway to set the control to continuously moves when direction is held? great tutorial btw!
ReplyDeleteHi and thanks, sorry for the late reply. From the top of my head you could do this in the Update method:
DeleteVector3 direction = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
transform.position += direction * speed * Time.deltaTime;
Speed can be something like 5 or 10. Experimenting gets the best result. This is not turn based however. I will have to look into writing a tutorial for continuous movement. Thanks for mentioning!
Thanks for writing this! There aren't enough text tutorials for Unity3D. Much appreciated :)
ReplyDeleteNo prob! Thanks for reading.
DeleteI'm having overall difficulty with this, could you maybe spare a few minutes next week to talk me trough a bit of the basics? If so I would greatly appreciate it. If not I understand, and just so you know, I'm BRAND new to coding in general so the fact that I'm confused is no comment on the effectiveness of your tutorial. It's rather well put together.
ReplyDeleteI stumbled across this tutorial and found it very useful. How come you didnt keep going with it?
ReplyDelete