Enemy projectile is not precisely fired at players position

Godot Version

<stable4.2>

Question

<Hello. I would appreciate if you could teach me a reason why enemy’s projectile is not being fired at player’s position precisely.

ezgif-6-c1a519c2fc

func _shoot():
	var mage_ball = mageballScene.instantiate()
	mage_ball.global_position = $MageBallSpawner.global_position
	mage_ball.direction = global_position.direction_to(player.global_position)
	get_parent().add_child(mage_ball)```
extends CharacterBody2D

var speed := 125
var max_range := 300.0
var _travelled_distance = 0.0
#var direction = Vector2.ZERO

func _init():
	set_as_top_level(true)

func _physics_process(delta: float) -> void:
	var distance := speed * delta
	var motion : Vector2 = direction * speed * delta
	position += motion

show your player’s scene? does the player sprite and other nodes positioned at exactly the center?

yes. player is positioned exactly at the center.
If I put var direction = global_position.direction_to(player.global_position) inside projectile script, the projectile will follow player and comes at the center of player like homing missile. So yes, the player is exactly positioned at the center.

try change to this

mage_ball.direction = mage_ball.global_position.direction_to(player.global_position)
1 Like

Thank you very much. That fixed the problem. I really appreciate your help.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.