Attention | Topic was automatically imported from the old Question2Answer platform. | |
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