0 votes

I have a Playercode:

extends KinematicBody2D

var cooldown = 0
onready var BULLET = preload("res://Bullet.tscn")


func _process(delta):
        if Input.is_action_just_pressed("ui_right"):
            if cooldown == 0:
                var bullet =  BULLET.instance()
                get_node("/root/World").add_child(bullet)
                cooldown = 1
                $Cooldown.start()

func _on_Cooldown_timeout():
          cooldown = 0

and a Bulletcode:

extends KinematicBody2D

var movement = Vector2(0,0)
var speed = 150


func _physics_process(delta):
    movement = Vector2(speed,0)
    move_and_collide(movement * delta)

And the Shootingsystem works, but I dont now how I can spawn the Bullets by the Player and not anywhere. So how can I spawn the Bullets by the Player?

in Engine by (289 points)

1 Answer

+1 vote
Best answer

Basically you want to set the transform of your bullet to the transform of your player.

So, after you create the bullet with this:

var bullet =  BULLET.instance()

Set its transform to that of the player

bullet.transform = transform

Note, this assumes the running code is on the player object, but I think it is... Also, you could attach some bullet spawn point to the player object (like right at the end of a weapon) and start your bullet there instead. It'd be basically the same thing, except you'd be setting the bullet's transform to the transform of that "spawn" node instead.

by (10,750 points)
selected by

Thanks it works! :)

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.
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 webmaster@godotengine.org with your username.

Categories