Gun shooting script

Godot Version

4.3

Question

`This problem might be too easy but ye i cant figure it out , I’m having a player script that spawns a projectile scene (bullet) whenever mouse left button is clicked, the spawn position of the bullet is handled via a marker2D node here is the scripts:

player script:
extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@export var projectile_scene : PackedScene
@export var fire_rate: float = 0.2
var can_shoot : bool = true

func _input(event):
if event.is_action_pressed(“shoot”) and can_shoot:
shoot()
func shoot():
if not can_shoot:
return

var projectile = projectile_scene.instantiate()
projectile.position = $Marker2D.global_position
var mouse_position = get_global_mouse_position()
projectile.direction = (get_global_mouse_position() - $Marker2D.global_position).normalized()
add_child(projectile)
print("Marker2D Position: ", $Marker2D.global_position)
print("Projectile Position: ", projectile.position)

can_shoot = false
await get_tree().create_timer(fire_rate).timeout
can_shoot = true

func _physics_process(delta: float) → void:
velocity.y = 0
if not is_on_floor():
velocity += get_gravity() * delta
var direction := Input.get_axis(“ui_left”, “ui_right”)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()

and here is the projectile script:
extends Node2D
@export var speed: float = 500.0
@export var lifetime : float = 2.0
var direction: Vector2 = Vector2.ZERO
func _ready() → void:
await get_tree().create_timer(lifetime).timeout
queue_free()

func _process(delta: float) → void:
position += direction * speed * delta

What is the problem? What do you expect to happen vs what really happens?

Make sure to paste with proper formatting

2 Likes

sorry im new here so i didnt know , the problem is what i expected to happen is the bullet spawns in the same position of the marker2D and then start moving but its not spawning in the same position of the market2D

Here is the scripts with the right foramtting:

player :



const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@export var projectile_scene : PackedScene
@export var fire_rate: float = 0.2
var can_shoot : bool = true


func _input(event):
	if event.is_action_pressed("shoot") and can_shoot:
		shoot()
func shoot():
	if not can_shoot:
		return

	var projectile = projectile_scene.instantiate()
	projectile.position = $Marker2D.global_position
	var mouse_position = get_global_mouse_position()
	projectile.direction = (get_global_mouse_position() - $Marker2D.global_position).normalized()
	add_child(projectile)
	print("Marker2D Position: ", $Marker2D.global_position)
	print("Projectile Position: ", projectile.position)

	can_shoot = false
	await get_tree().create_timer(fire_rate).timeout
	can_shoot = true
func _physics_process(delta: float) -> void:
	velocity.y = 0



	move_and_slide()

here is the projectile script :

@export var speed: float = 500.0
@export var lifetime : float = 2.0
var direction: Vector2 = Vector2.ZERO
func _ready() -> void:
	await get_tree().create_timer(lifetime).timeout
	queue_free()


func _process(delta: float) -> void:
	position += direction * speed * delta
2 Likes

Ah I see you may be mixing global and local positions. Try setting the global_position

projectile.global_position = $Marker2D.global_position

Here it may be better to use add_sibling(projectile), if the projectile is a child of the player it will move with them even after being shot. i.e. walking the player left shouldn’t move all of their bullets left as well.

2 Likes

tysm man this actually fixed my problem :heart:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.