0 votes

Hey guys!
I had a ship (RigidBody2D) that shot a beam (Area2D). But when the ship rotate the beam still goes in the same direction.

The code of the Ship:

func Fire():
var new_shot = shot.instance()

get_parent().add_child(new_shot)
new_shot.User = self
new_shot.set_dir(0, -1)
new_shot.position = $Position2D.global_position

And the code of the Shot:

var dir = Vector2 ()
var Power = 1
export var Speed = 50
var vel = Vector2()
var User

func set_dir(x, y):
    if y > 0:
        $AnimatedSprite.flip_v = true
    dir = Vector2(x, y)

func _process(delta):
    vel = dir * Vector2(Speed, Speed) * delta
    translate(vel)
Godot version 3.2.3
in Engine by (72 points)
edited by

1 Answer

–1 vote

From what I understood, I think you have to rotate you vector by your ship's rotation in set_dir() or it will always shoot UP at Vector2(0, -1)

dir = Vector2(x, y).rotated(your_ship_node.rotation)

So if your ship is User:

dir = Vector2.(x, y).rotated(User.rotation)

Also you can use Vector2.ZERO, Vector2.UP, Vector2.LEFT etc... instead of Vector2(0, 0), Vector2(0, -1)... it's less confusing I guess ;)

by (91 points)
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.