This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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 (22,191 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 Frequently asked questions and 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 [email protected] with your username.