How can i make a 2D body go forwards when it rotate?

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

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)
:bust_in_silhouette: Reply From: leo-pnt

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 :wink: