0 votes

So, I am trying to make my first game, and its a galaga clone. However, I am running into some problems. I have 3 scenes, the player, the bullet, and the main scene.

The main scene just contains instances of the bullet and player

The player's tree is this:

  Area2D(named Player)
         CollisionShape2D
         Sprite

The bullet's scene tree is this:

   Area2D(Named Bullet)
         Sprite
         CollisionShape2D
         VisibilityNotifier2D

Here is the script for the player:

extends Area2D

export var playerSpeed = 400
var screen_size

func _ready():
    screen_size = get_viewport_rect().size

func _process(delta):
    var Playervelocity = Vector2()
    if Input.is_action_pressed("ui_right"):
        Playervelocity.x += 1
    if Input.is_action_pressed("ui_left"):
        Playervelocity.x -= 1
    position += Playervelocity * delta * playerSpeed
    position.x = clamp(position.x, 0, screen_size.x)

Here is the bullet script:

extends Area2D

export var bulletSpeed = -500

func _on_VisibilityNotifier2D_screen_exited():
    queue_free()


func _process(delta):
    position += Vector2(0.0, bulletSpeed * delta)

Here is the Main script:

extends Node2D

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

func _process(delta):
    if Input.is_action_just_pressed("ui_select"):
        var bullet = Bullet.instance()
        add_child(bullet)
        $Bullet.position = $Player.position

So, my problem is that when I shoot multiple bullets, and I'm not at the spawn point for the player, the majority of the bullets shoot out from the player's spawn point and one shoots from the player, and every time you press space, the bullet that is shooting from the player teleports back to the player.

asked May 20 in Engine by Fizzleburn (20 points)

1 Answer

0 votes
Best answer

It should be

bullet.position

Instead of $Bullet which refers to one specific bullet

answered May 20 by A112Studio (178 points)
selected May 20 by Fizzleburn

If i do that, then it gives me the error "Invalid index 'position' (on base: Packed Scene) with value of type 'Vector2'"

Never, mind, capitols matter.... lol THNX

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 How to use this Q&A? before posting your first questions.