0 votes

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
    owner.add_child(Bullet_Instance)

Bullet Code:

extends KinematicBody2D

var angle = 0

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

func _physics_process(_delta):
    move_and_slide(direction*500)

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.

Godot version 3.3.2
in Engine by (15 points)

1 Answer

+1 vote
Best answer

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:

ship:

Bullet_Instance.rotation = rotation

bullet:

move_and_slide(transform.x * 500)
by (20,355 points)
selected by
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.