0 votes

I want to move all instances (after being spawned) by some distance each time my player shoots a bullet.

extends KinematicBody2D

var motion = Vector2(10,0)
var current_pos = self.global_position # saved current position

func _ready():
    self.global_position = Vector2(85, 490)
    #starting position when spawned
func _process(delta):
    if Global.is_step == true:#true when player shoots a bullet
        current_pos = self.global_position 
        # save current position each time for each instance
        Global.is_step = false

func _physics_process(delta):
    if self.global_position.x < current_pos.x + 100:
        self.global_position += motion

I wanted to spawn each instance one by one when the player shoots. But the above code works only for the first instance produced and the rest of them stay in the position where they were spawned. I tried using 'self' but it still doesn't work. I am a beginner godot game developer so I'd like you to help me out with this.

in Engine by (18 points)

1 Answer

+1 vote
Best answer

But the above code works only for the first instance

Because it has the highest position in the tree, that instance calls -process() first. And sets Global.is_step back to false, before the other instances are processed.

Whatever part of your code sets Global.is_step to true should also be responsible for setting it back to false after X frames (10 in your example).

by (10,608 points)
selected by

Thank you for helping me.

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.