How to spawn the bullet by the Player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

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?

:bust_in_silhouette: Reply From: jgodfrey

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.

Thanks it works! :slight_smile:

Godot_Starter | 2020-02-17 21:01