mardi 18 juillet 2023

How can I track down desired direction in 2D space?

So let's say we have a bullet that have a wide and random spray pattern in a 2D space. When we shot it flies until one second is passed, then returns to start position. Here is the code for this

var velocity = Vector2()
var shot

func _physics_process(delta):
    if Input. is_action_pressed("ui_right") and not shot:
            velocity.x = 1000
            velocity.y = rand_range(-250,250)
            shot = true
            $Timer.start()
    
    velocity = move_and_slide(velocity * direction)

func _on_Timer_timeout():
    shot = false
    position = Vector2(500, 250)
    velocity = Vector2(0,0)

Here is the problem: when bullet flies to a random point it needs to face this point.

I want it to change directions according to the place it moves to so I need help. How can I do this?




Aucun commentaire:

Enregistrer un commentaire