What am I missing here to make sure that the rocket is always pointing in the direction where I am shooting at?
Just as an extra note, right now the rocket is fire-and-forget but at a later stage I also want to enable it to home in on a target, just in case if that makes any difference to the solution.
Yes because global transforms only make sense and work if there is a global space of the scene tree.
Do you want it to fly forward or home the enemy?
In any case you need to set the global directionVector properly. Btw no need to split it into components. Simply do global_translate(directionVector * speed * delta)
If you oriented it by copying the marker basis then just copy the forward global basis vector which is presumably basis z.
Each frame calculate the direction vector from the projectile to the enemy. Construct a basis using that vector and global up and rotate the projectile basis for a small amount towards the constructed basis.
func _process(delta: float) -> void:
var enemies = get_tree().get_nodes_in_group("enemy")
var enemy = enemies.front() as Node3D
if enemy:
#what is the vector towards my enemy
var targetVector = (self.global_position - enemy.global_position)
#what is the actual angle
var direction_angle = Vector2(targetVector.x, targetVector.z).angle_to(Vector2.UP)
#adjust rotation in increments
rotation.y = rotate_toward(rotation.y, direction_angle, delta * rotation_speed)
DebugDraw3D.draw_arrow(self.global_position, enemy.global_position, Color(0.0, 0.553, 0.0, 1.0), 0.8, true)
#setting thrust vector based on new rotation
directionVector = self.global_basis.z
#moving the object
global_translate(Vector3(delta * speed * directionVector))
lifetime -= delta
if lifetime <= 0:
self.queue_free()
Sounds like a good idea for an upgrade option to the RPG! I was also wondering to add a little bit of randomness to the flightpath so that it kinda ‘wiggles’ it’s way to the target
You could have a variable and make the rocket’s rotation.y += sin(whatever variable) then increment that variable each frame to make it wiggle along a sine wave