How to make enemy shoot at player ?

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

hello guys, I am making just a shooter game and I have managed to make an enemy that moves and shoots bullets in one direction using this :

export var rocket = preload("res://MainScenes/enemyRocket.tscn")
func fire():
	var roc = rocket.instance()
	get_tree().get_root().add_child(roc)
	roc.global_position = global_position

I call this fire function in the func _process and I have some other statements that manage the timings of fire rate etc. What I want to know is that how can I make the enemy fire at my ship instead of just one place. Can I add something in the above code to do that ?

I managed to make the bullets point in my direction using these :

var dir = (get_player().global_position - global_position).normalized()
roc.global_rotation = dir.angle() + PI / 2.0

But this just makes the bullets point towards me. What can I do to actialy make them come towards me ?

how are you handling the movement of the rocket?

p7f | 2020-09-06 15:25

func _process(delta):
	translate(velocity*delta)
	

Simply like this. I have no idea how I can make the rocket go at player’s position

Scavex | 2020-09-06 15:41

Is velocity a vector? you just need to rotate that vector to point to the character.

you could do something like:

var direction = glonal_position.direction_to(player.global_position)

and then use that direction to get the velocity, like velocity = speed*direction or something like that.

p7f | 2020-09-06 16:07

Should I do that in my rocket script or in the script of enemy that fires the rocket ? Also player is not recognized so should I load it in the script like this :

var player = load("res://path to player scene")

I tried loading it in the rocket script but got an error saying that invalid get index ‘global_position’ (on base: PackedScene)

Scavex | 2020-09-06 16:19

:bust_in_silhouette: Reply From: p7f

Given your current code, i would do this.

  1. In your Rocket script, add a variable called direction
  2. In your enemy script, change the direction of the rocket, similar to how you change its rotation.
  3. In your Rocket script, calculate the velocity, with the given direction…

For example, in the enemy script:

export var rocket = preload("res://MainScenes/enemyRocket.tscn")
func fire():
    var roc = rocket.instance()
    get_tree().get_root().add_child(roc)
    roc.global_position = global_position
    var dir = (get_player().global_position - global_position).normalized()
    roc.global_rotation = dir.angle() + PI / 2.0
    roc.direction = dir

And in your rocket script:

var direction : Vector2 = Vector2.LEFT # default direction
var speed : float = 100 #put your rocket speed

func _process(delta):
    translate(direction*speed*delta)

It can have mistakes, just try it and tell me!

You sir, are a true legend ! So glad that you helped and replied so fast. Angel

Scavex | 2020-09-06 16:40

Glad to help!

p7f | 2020-09-06 16:53