Trying to get shooting to work in a side scroller

Godot Version

Godot 4.2.2

Question

I’ve been taking a break from my first game for a while and I just returned to it today, facing the same problem which made me quit: the shooting just refuses to work.

The problem is this: when I test the game, the game crashes when I press the shoot button and shows this error message: “Invalid set index ‘position’ (on base: ‘Sprite2D (shot.gd)’) with value of type ‘float’.”

I’ll include the scripts for the player and the shot that I’ve made so far plus the error message in the debugger.





Just delete the line that causes this issue. Not only it cannot work this way because you are trying to assign a float (direction) to a vector (position), but you already set the global_position in the next line, so it’s not necessary to set the (local) position too.

Anyway, if you want to keep that line for any reason, it should probably look like this: shot_instance.position = muzzle.position

I see. I did already try deleting the line by making the code like this:

func shoot():
	var shot_instance = shot.instantiate() as Node2D
	shot_instance.global_position = muzzle.global_position
	get_parent().add_child(shot_instance)

Now the shoot button works, but it just creates a shot/bullet next to the player character; it doesn’t shoot the shot like it’s supposed to.