Why are my bullets not working ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Scavex

Hello guys. First of all I feel very sad that GODOT messed my project .Everything was fine until I deleted on of my scenes intentionally and the next time I started Godot It gave an error that dependencies not found and due to that I had to again make my Stage 1. coming to the question, prior to the above incident my player was actually able to fire bullets properly (almost since the Position2D wasn’t working when I was placing it in front of my player’s collision shape so I placed it on my player’s head) but now the bullets are not getting fired and when my player falls only then I see a few bullets coming out from my player.
This is my Player Code

extends KinematicBody2D

const UP=Vector2(0,-1)
const GRAVITY=20
const MAX_SPEED=150
const JUMP_HEIGHT=-400
const BULLET=preload("res://Bullet.tscn")
var motion =Vector2()

var playerhp=10

var is_dead=false

func _physics_process(delta):
	if is_dead==false:
		var friction=false
		if Input.is_action_pressed("ui_right"):
			if sign($Position2D.position.x)==-1:
		elif Input.is_action_pressed("ui_left"):
			if sign($Position2D.position.x)==1:
		if is_on_floor():
			print("On floor,")
			if Input.is_action_just_pressed("ui_up"):
			if friction == true:
			if friction == true:
		if Input.is_action_just_pressed("ui_focus_next"):
			var bullet=BULLET.instance()
			if sign($Position2D.position.x)==1:
		if get_slide_count()>0:
			for i in range(get_slide_count()):
				if "Enemy" in get_slide_collision(i).collider.name:

func dead():
	if playerhp<=0:

func _on_Timer_timeout() -> void:

func _on_VisibilityNotifier2D_screen_exited() -> void:

This is my code for Bullet

extends Area2D

const SPEED =300
var velocity = Vector2()
var direction=1

func _ready():

func set_bullet_direction(dir):
	direction = dir
	if dir==-1:

func _physics_process(delta):

func _on_VisibilityNotifier2D_screen_exited() -> void:

func _on_Bullet_body_entered(body: Node) -> void:
	if "Enemy" in body.name:

So I want to ask that it was almost working before but why isn’t it working now. One thing I also want to mention is that I created a separate scene for my PLAYER and added it as an instance to my Stage1 Scene. Please help me out since it’s frustrating me.

:bust_in_silhouette: Reply From: njamster

When the player is pressing ui_left, your script is setting the position.x of your Position2D-node to 1 instead of inverting the value. It needs to be:

if sign($Position2D.position.x) == 1:
    $Position2D.position.x *= -1

Furthermore a bullet is always freed when _on_Bullet_body_entered is called, even if the entering body is not an enemy. So this is what happens: You walk around and your code fucks up the muzzle-position, the muzzle is now on top of the player. And the player is a body, too! So if you now spawn a bullet, the body_entered-signal will be fired and the bullet is getting freed immediately before you can see it.

On an unrelated note: the whole friction-part of your player-script is almost certainly not working as you imagine it to work! By first resetting motion.x to zero, all linear interpolation afterwards is meaningless (as the current value and goal are already the same, nothing to interpolate anymore). But if you wouldn’t do that, you’d interpolate two times each frame when the player doesn’t provide any inputs.

Everything was fine until I deleted on of my scenes intentionally and the next time I started Godot It gave an error that dependencies not found and due to that I had to again make my Stage 1.

ALWAYS backup your work - no software is perfect! It may take a while to get used to version control software like git, but I guarantee you it’s worth it!

That being said, an error about missing dependencies after you deleted something sounds a lot like you accidentally deleted something that was required somewhere else as well. If that’s not the case, it’s usually easy to fix manually.

I’m so glad you answered but the issue prevails. The weird thing that I just realised is that the bullets were working all along but the position of Position2D node that I have kept in front of my Player in the editor of GODOT is not the position where the bullets are getting fired from when I run the game. For example let’s say that (10,0) are the coordinates where I have kept my Position2D crosshair but in the game (Just an example) Position2D is at (10,-80). So when I keep the Position2D way above my player instead of directly front of him then in the game I see the bullets getting fired from front of him. And still when I turn to left the bullets start from his inside and end right there. Only works fine when he’s facing right.

Scavex | 2020-04-15 15:00

That’s weird. Your code worked fine for me after the two changes I mentioned. Can you upload an example project somewhere and provide a link here? Otherwise I am afraid I won’t be able to help you any more, sorry.

njamster | 2020-04-15 18:20

This is google drive link to my project folder.

Please take a look at it and tell me what’s wrong.

Scavex | 2020-04-16 07:38

In your player script, you’re doing


when what you should do instead is


The former is using the global (i.e. absolute) position of the Position2D as a local position (i.e. relative to the parent, Stage1 in your case) for the bullet.

njamster | 2020-04-16 11:11

Thank you so much for you time and efforts to help me with this problem. It solved everything. God Bless you !

Scavex | 2020-04-16 12:26