This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 ?

in Engine by (418 points)

how are you handling the movement of the rocket?

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

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

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.

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)

1 Answer

+4 votes
Best answer

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!

by (3,505 points)
selected by

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

Glad to help!

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 Frequently asked questions and 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 [email protected] with your username.