0

I'm new at Godot and I want to develop a small 2d tank game. But I'm stuck at shooting. I've watched many tutorials, but they are for me beginner far too complicated. So I want to ask if it's possible to move an object into a direction with just an angle to prevent using coordinates.

Until now I have programmed that if space is pressed the bullet appears and if it touches something, it dissappear and create an event

ekcum
  • 1
  • 1

1 Answers1

0

I argue in another answer that you should use Area(2D/3D) for projectiles.

The Area(2D/3D) has a global_position that you can use to move it.

Since you want to work with an angle, you can define a vector in the appropriate direction like this:

var direction := Vector2.RIGHT.rotated(angle)

You will also need a speed:

var direction := Vector2.RIGHT.rotated(angle)
var velocity := direction * speed

And then you can move your Area2D by that:

func _physics_process(delta:float) -> void:
    global_position += velocity * delta

I hereby remind you that velocity is displacement over time, and delta is the elapsed time between frames. So when you multiply velocity by delta you get displacement... Which is the change in position.


If for whatever reason your projectile is a kinematic body, you would just set its velocity, and then call move_and_slide in _physics_process.


You are going to use vectors and you are going to like it.

Theraot
  • 31,890
  • 5
  • 57
  • 86