Changing child variables from the parent without them being reset

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

I’m new to godot and I was trying to make a asteriods type game. But i’m having trouble getting my space ship to shoot.
The issue is that I can’t get the bullet to fire in direction of the ship. I set an ‘angle’ variable in the ship and assign it to the bullet once it has been instanced, but i’m not sure how to use it in the bullet. The bullet will just move right no matter the angle of the ship.

Ship Firing Code:

func _process(_delta):
if Input.is_action_just_pressed("action_fire"):
	var Bullet_Instance = Bullet.instance()
	Bullet_Instance.angle = angle

Bullet Code:

extends KinematicBody2D

var angle = 0

var direction = Vector2(cos(angle), sin(angle))

func _physics_process(_delta):

I’m pretty sure that the angle variable is being reset to 0 in the bullet code after it has been set in the ship code, but i’m not sure how to fix this. Thanks.

:bust_in_silhouette: Reply From: kidscancode

You’re setting the direction of your bullet before the bullet is spawned. Changing the value of angle isn’t going to change that.

You really don’t need the angle variable in the first place. Nodes already have a variable that keeps track of their angle: rotation. Then you can just have the bullet move forward, no matter what direction it’s facing, by using its transform:


Bullet_Instance.rotation = rotation


move_and_slide(transform.x * 500)