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!