The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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 ACCELERATION=50
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:
        motion.y+=GRAVITY
        var friction=false

        if Input.is_action_pressed("ui_right"):
            motion.x=min(motion.x+ACCELERATION,MAX_SPEED)
            $Sprite.flip_h=false
            $Sprite.play("Run")
            if sign($Position2D.position.x)==-1:
                $Position2D.position.x*=-1
        elif Input.is_action_pressed("ui_left"):
            motion.x=max(motion.x-ACCELERATION,-MAX_SPEED)
            $Sprite.flip_h=true
            $Sprite.play("Run")
            if sign($Position2D.position.x)==1:
                $Position2D.position.x=-1

        else:
            motion.x=0
            $Sprite.play("Idle")
            friction=true
            motion.x=lerp(motion.x,0,0.2)

        if is_on_floor():
            print("On floor,")
            if Input.is_action_just_pressed("ui_up"):
                motion.y=JUMP_HEIGHT
            if friction == true:
                motion.x=lerp(motion.x,0,0.2)

        else:
            $Sprite.play("Jump")
            if friction == true:
                motion.x=lerp(motion.x,0,0.5)

        if Input.is_action_just_pressed("ui_focus_next"):
            var bullet=BULLET.instance()
            if sign($Position2D.position.x)==1:
                bullet.set_bullet_direction(1)
            else:
                bullet.set_bullet_direction(-1)
            get_parent().add_child(bullet)
            bullet.position=$Position2D.global_position
        motion=move_and_slide(motion,UP)

        if get_slide_count()>0:
            for i in range(get_slide_count()):
                if "Enemy" in get_slide_collision(i).collider.name:
                    dead()

func dead():
    playerhp-=1
    if playerhp<=0:
        is_dead==true
        motion=Vector2(0,0)
        $Sprite.play("Dead")
        $CollisionShape2D.disabled=true
        $Timer.start()







func _on_Timer_timeout() -> void:
    get_tree().change_scene("StartMenu.tscn")


func _on_VisibilityNotifier2D_screen_exited() -> void:
    get_tree().reload_current_scene()

This is my code for Bullet

extends Area2D

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

func _ready():
    pass

func set_bullet_direction(dir):
    direction = dir
    if dir==-1:
        $Sprite.flip_h=true


func _physics_process(delta):
    velocity.x=SPEED*delta*direction
    translate(velocity)


func _on_VisibilityNotifier2D_screen_exited() -> void:
    queue_free()


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

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.

in Engine by (418 points)

1 Answer

+1 vote
Best answer

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.

by (10,634 points)
selected by

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.

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.

This is google drive link to my project folder.

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

In your player script, you're doing

bullet.position=$Position2D.global_position

when what you should do instead is

bullet.global_position=$Position2D.global_position

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.