Godot 3.2.3 Grid Movement Part 1/2

I converted this Godot Grid Movement Video Tutorial into textual tutorial because I feel it is much easier to follow textual tutorial.

What You Will Learn From This Godot Grid Movement Tutorial

  1. How to create player sprite that move in 2D grid
  2. Create Collision walls where the player sprite cannot pass through the wall

Part 1 – Make the Sprite Move

Create Node

i. Click “Other Node”
ii. Then select “Node”

godot grid movement - create node
godot grid movement – create node
godot grid movement - select node
godot grid movement – select node

Create Sprite

i. Right click “Node” -> Add child node
ii. Select Sprite
iii. Drag and drop icon.png into sprite texture at right panel
iv. At sprite panel, untick centered on
v. Click “Scene” (top menu left) then save scene as player.

godot grid movement - create sprite
godot grid movement – create sprite
godot gride movement - drag n drop icon to sprite texture
godot gride movement – drag n drop icon to sprite texture
godot grid movement - untick centered
godot grid movement – untick centered
godot grid movement - save scene as player
godot grid movement – save scene as player

Add Script to the Player sprite

i. Right click on Sprite -> Attach Script
ii. Click create on when the window open.
iii. Remove all codes except “extends Sprite”
iv. Copy the code below and paste to the script. Ensure indentation is correct.

godot grid movement - attach script
godot grid movement – attach script
godot grid movement - create script
godot grid movement – create script
godot grid movement - remove all codes except extends Sprite
godot grid movement – remove all codes except extends Sprite
extends Sprite


var speed = 256
var tile_size = 64

var last_position = Vector2()
var target_position = Vector2()
var movedir = Vector2()

func _ready():
	position = position.snapped(Vector2(tile_size, tile_size))
	last_position = position
	target_position = position
	

func _process(delta):
	#MOVEMENT
	position += speed * movedir * delta
	
	if position.distance_to(last_position) >= tile_size - speed * delta:
		position = target_position
		
	
	#DELTA
	if position == target_position:
		get_movedir()
		last_position = position
		target_position += movedir*tile_size
		

func get_movedir():
	var LEFT = Input.is_action_pressed('ui_left')
	var RIGHT = Input.is_action_pressed('ui_right')
	var UP = Input.is_action_pressed('ui_up')
	var DOWN = Input.is_action_pressed('ui_down')
	
	movedir.x = -int(LEFT) + int(RIGHT)
	movedir.y = -int(UP) + int(DOWN)
	
	if movedir.x != 0 && movedir.y != 0:
		movedir = Vector2.ZERO
		

godot grid movement - script part 1
godot grid movement – script part 1
godot grid movement - script part 2
godot grid movement – script part 2

Run the Scene

i. Click “Play” (top right) or F5 to run the scene.
ii. Move your arrow left, right, up and down to move the player sprite.

Part 2 Godot Grid Movement Tutorial – Make The Collision Wall and Combined with Player Sprite