Setting global position properly

4.3

Set bullet position in an Asteroids-like game

Hello there!

First time Poster and new to game programming altogether and I have a little project for work. I started following different turoials on how to create an asteroids clone with godot, but after some time I realized, using the pieces from different tutorials can make code & code-snippets unusable.

I restarted the project over, using a single tutorials that has most of the boxes checked we need for our project. There’s just one or two small things I tried to change myself, especially the movement:

In the tutorial (by CoffeeCrow, idk if it’s okay to name that here) they are using mouse inputs for direction, with acceleration & deceleration, whereas I want it to be fully keyboard movement, which I did in the following way:

func _physics_process(delta):
var input_vector := Vector2(0, Input.get_axis(“move_forward”, “move_backward”))

velocity += input_vector.rotated(rotation) * acceleration
velocity = velocity.limit_length(max_speed)

if Input.is_action_pressed("rotate_right"):
	rotate(deg_to_rad(rotation_speed*delta))
if Input.is_action_pressed("rotate_left"):
	rotate(deg_to_rad(-rotation_speed*delta))

if input_vector.y == 0:
	velocity = velocity.move_toward(Vector2.ZERO, 3)


move_and_slide()

Now when it comes to spawning the projectiles, they use global_mouse_position to get the direction of the projectiles. As I didn’t want to switch too much around, I thought I could just switch around the definition of position to my way of movement. Could you help me out, on how to incorporate these changes?

For reference, the code used for the direction to shoot in:

func fire_weapon() → void:
if Input.is_action_just_pressed(“fire”):
var mouse_position : Vector2 = get_global_mouse_position()
var entity_container : Node2D = get_tree().get_first_node_in_group(“entity_container”)

	if entity_container == null:
		return
		
	var new_projectile = projectile_packed_scene.instantiate() as BaseProjectileEntity
	
	new_projectile.set_direction((mouse_position - global_position).normalized())
	new_projectile.set_speed(TEST_SPEED)
	new_projectile.rotation = new_projectile.direction.angle()
	
	entity_container.add_child(new_projectile)
	
	new_projectile.global_position = primary_weapon_anchor.global_position

I hope it’s fine to post this here and I’m happy for any help!
Thanks!

I can’t see anything wrong here. If you intend the movement to be keyboard dependent and mouse independent, and the attack to be mouse dependent and keyboard independent, this should work. Did you try running the project? Let me know if you get any errors or bugs.

Thanks for the quick reply!

It works exactly like you mentioned, but I don’t want my attack to be mouse dependent, but player dependent on my current position.

Sorry, I didn’t make myself clear enough, I want it to follow the direction of the player model and the direction im facing, not controlling it with the mouse.

Do you mean you want it to work such that the bullets fire in the player’s direction? I.e. instead of firing at the mouse position you fire it in the direction the player is facing?

1 Like

Yes, correct. Completely independent from mouse controls, only using WASD movement and spacebar for shooting.

Set the bullet’s rotation equal to the player’s rotation

Can you give me a hint at the documentation or which prompt I’d use for that?
I’m completely new and working based off of tutorials right now.

But when I have the right syntax, I think I can work something out.

Oh right, I forgot you also need to add the bullet to the scene when the spacebar is pressed and not when the mouse is clicked.

For some documentation, most of what you need is given in Your first 2D game — Godot Engine (stable) documentation in English
Follow it and you should know how to handle inputs and instantiate scenes. Tell me if there’s something you need you didn’t find there

In general however, I recommend reading a good chunk of the basics of the manual, otherwise you’ll come here every few minutes making the game.