
We now need to look at creating a spawner script which will initially spawn a bunch of enemies randomly at a random position. We want to setup something similar to the bullet node structure. Let us start off by creating a new scene again call Enemy.tscn. So for our enemy I created this graphic in inkscape. We now have a shooting bullet let us consider maybe how we can use godot to spawn enemies in a random position or at a set position. You should end up with something like this when you run this.

Due to the script on the Projectile gdscript this will control the movement for our bullet. Then finally we add it to the root node of our game. The shoot bullet function basically loads up our Projectile scene we then instance it. So we basically do a check for the left mouse button and trigger the shoot function. The important part of this script is the shoot function and the part where we trigger it off the mouse click. Once we have done that we need a get_input function where we will check for the right,left,up and down arrow key as well as the left mouse button.
Construct 3 spawn another object code#
In the code we start off with declaring a velocity for our player 2d 4 way movement. Var projectile = load("res://Projectile.tscn")Īdd_child_below_node(get_tree().get_root().get_node("Game"),bullet) If Input.is_mouse_button_pressed(BUTTON_LEFT): So we need to create a player script which can do some basic movement and have a trigger to shoot. Next we need a way to trigger the bullet to spawn. Character movement script and bullet spawn trigger Then we call move and slide to move our bullet. In our _ready function we simply assign a y velocity to shoot our bullet straight up. Then we declare a velocity which will be used to control the velocity of our bullet.

extends KinematicBody2Dįirst we will extend the KinematicBody2D class so we can access move_and_slide. Then in our script we will be using this code. Once you have created this attach it to your Projectile KinematicBody2D. Once you have this setup we need to create a GDScript called projectile. You will want to replicate this node structure to make it work. So go ahead an create a new scene and save it as Projectile.tscn. To get this working we are going to start off with a new scene where we can create a bullet sprite and some bodies to allow us to shoot our bullet at a velocity. I created a very basic rectangle image which we will use for our bullet. To spawn a bullet we first need a bullet. Godot Spawn Object: Godot spawn bullet sprite So let’s jump into making godot spawn a bullet.

Let us create a way for our player to shoot a bullet. Let’s do something a little more fun now. So if you have done this correctly you should end up with. You can go ahead and attach this to your Game node as a script like so. We are now going to modify this script to. When you open it up you should have this. Ok so now on the FileSystem right click and create new script. Once done we can now add a script for spawning our player, but first before we do that hit play to just check that the screen is showing blank and that the game.tscn is the main scene. Which at this point will only have a Node2D which I renamed to Game. Now we going to create a new scene called Game.tscn. Once you have that save your player as Player.tscn. So you can just replicate what I have below. So for simplicity sake I have given my player a KinematicBody2D and CollisionShape2d for when we expand our project further. For this I am going to use the basic player icon.png in most godot projects when you first create your project. To spawn a player in godot we need to just start by setting up a player scene which we can use.
