Player and Enemy

By: Nathan Lovato - June 20, 2020

Learn to code the player and enemy of our 2D game from scratch.

If you haven’t already, you can download the project’s assets on the project’s repository (look for the file start-assets.zip).

Errata

Missing segment at 32:21

There is a missing segment at 32:21, where I changed the project’s window settings. These settings control the resolution and the way the game window scales. Here’s how to change them.

Go to the menu Project -> Project Settings.

In the left column, scroll down to Display -> Window, and click on Window to open the window settings on the right side.

Here are the settings I changed. In the size section at the top. These settings control the resolution of the game and size of the window:

  • Width: 1920
  • Height: 1080
  • Test Width: 1280
  • Test Height: 720

Then, scroll down to the stretch section and set:

  • Mode: 2d
  • Aspect: expand

These two settings will make the game viewport resize with the window. Without them, when you increase the window size, more of the game level would show. The expand aspect setting makes it so when you change the ratio of the window, it shows a little more or less of the game world to preserve the proportions of the game sprites. You can play with these settings to see the differences they make.

Change in value at 1:02:26

At 1:02:26, the line 13 of Player.gd changes: the value 1.0 turns into 0.0, but it’s not mentioned in the video.

You should also change this value. The line should be:

-Input.get_action_strength("jump") if is_on_floor() and Input.is_action_just_pressed("jump") else 0.0

Made by

Nathan Lovato

GDQuest founder. Courteous designer with a taste for Free Software. I promote sharing and collaboration.

15 comments

Hao Huynh

I can’t make the enemy die when I jump on it I copied your code entirely so I didn’t know where the issue was There was a few difference in the signal window though. it said “body_entered(body:Node)” However, I changed it back to Physics2D in the script And when you said to deactivate collision2D on the enemy, since I didn;t see you do it in the video so I didn’t know where to follow

extends “res://source/Actors/Actor.gd”

func _ready() -> void: set_physics_process(false) _velocity.x = -speed.x

func _on_StompDetector_body_entered(body: PhysicsBody2D) -> void: if body.global_position.y > get_node(“StompDetector”).global_position.y: return queue_free()

func _physics_process(delta: float) -> void: _velocity.y += gravity * delta if is_on_wall(): _velocity.x *= -1.0 _velocity.y = move_and_slide(_velocity, FLOOR_NORMAL).y

Reply to Hao Huynh

I’m having the same problem as HAO HUYNH and will report back if I find a solution.

I’m guessing, since this tutorial is almost a year old, that Godot updates changed the way some things are done.

Reply to Axel
if body.global_position.y < get_node("StompDetector").global_position.y: seems to do the trick : I simply changed the > for a <
Reply to Axel

That’s actually not the proper fix : the problem occurred due to bad positioning of the StompDetector and CollisionShape2D.

I lowered the StompDetector (so its origin is slightly closer to the center of the enemy) and stomping works well.

Reply to Axel
I’m unable to make my player jump with space bar, W, or the up arrow key. I went through the instruction and also copied/pasted the code from the example with no success. I also made sure gravity was set to 4k like the instructor did. My collision mask, and speed/direction variables have the same values. He just won’t jump.
Reply to Branden
Branden -

My player floats up into the air after going up an incline while the player with original source code doesn’t seem to. Would anyone have a thought as to why that is?
Reply to Branden -
hey, at ~1:20:00 into the video, after I check the process_parent & physics_process_parent boxes, the enemy stops moving whenever it’s off screen, even if it starts off screen.
Reply to henry
doesn’t really seem like it matters– I think we end up writing a line later on that makes that happen anyway and either way it doesn’t break the game by any means
Reply to Henry

Enter your email to get notified when someone replies to your comment.
We encrypt your addres with a strong 256-bit AES encryption.
We'll only use your address for notifications. You can unsubscribe anytime.