0 votes

Ok, so here is what my nodes look like right now:

KinematicBody (player)
     sprite
     collisonshape2D
     sprite (GUN)
           timer

Bullet.tscn is a separate scene with code that says:

extends KinematicBody2D
func _physics_process(delta):
    position.x += 10 

Right now, I have a script in the Gun that says:

extends Sprite

var Bullet = preload("res://Bullet.tscn") 

func shoot():
    var bullet = Bullet.instance()
    add_child(bullet)

func _process(delta):
    if Input.is_action_pressed("click"):
        shoot()

The bullets spawn and move, but when I move forward, the bullets move forward faster, like they've been boosted. When I move backwards, the bullets stop and slightly move backwards until I stop moving. Can someone please explain how to fix this problem?

in Engine by (12 points)

2 Answers

0 votes

So, the main problem is that you're adding the newly spawned bullet as a child of the Gun, which in turn, is a child of the Player. So, any motion applied to the Player, will also be applied to the Gun (expected) and the spawned bullet (not expected).

You need to add the bullet as a child to some other object that's not moving in your game. You could simply add the bullet as a child of your game world, or some other stationary container in your game world.

Bottom line, don't add into the hierarchy of the Player itself or it will move with the player.

by (14,952 points)

Thank you so much!

0 votes

you can use "setastoplevel(true)" within the ready function of your bullet in order to separate the bullets movement from the players.

by (14 points)
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.