How to shoot a fireball from two different Position2D placed near the player and make it go to the mouse direction

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

Hello guys, I just did an animated sprite for one of the spells i’d like to use. It lives under an area 2D node

extends Area2D

func _physics_process(delta: float) → void:

func _on_VisibilityNotifier2D_screen_exited() → void:

I placed two different Position2D nodes near the player and I’d like to get him to shoot those fireballs from the left node if facing left or shoot from the right node if facing right.

I also want the fireball to go to the position of the mouse.

I have tried instancing the fireball, but the main problem for me is making it choose a starting position and then moving towards the mouseposition. I also tried doing the whole script on my player’s gd file with no succes whatsoever.

May you please help me figuring this out?

:bust_in_silhouette: Reply From: jtarallo

Hi, so, the seudocode for this would be:

# in player script when handling input:
var origin = $RightPositionNode
if mouse_position.x - player.position.x < 0.00:
    origin = $LeftPositionNode

var fireball = FIREBALL.instance()
fireball.position = origin.position
fireball.targetPosition = mouse_position 
# add fireball to scene. you can do 
# it in player node or in its parent

# in fireball script:
var targetPosition:Position2D 
const SPEED = 300.00 # pixel/second
# be sure to have the set_physics_process(true) line in fireball's _ready func

# physics_process method:
# we move towards the targetPosition calculating 
# unit vector using current position and dividing it 
# by the vectors length
self.position += SPEED*delta*(targetPosition - position)/(targetPosition - position).length

If you have any further questions let me know, but I think I’ve covered both your doubts.
If you don’t have a clear knowledge of how to handle mouse and input coordinates, I leave you the documentation, you should start there before you try to implement what I answer.

Hope it helps

Thank you @Jtarallo.

I found an alternative solution to this I left the Positions idea behind and tried something different.


#shooting fulgorballs
if Input.is_action_just_pressed("Attack1") && is_attacking == false:
	is_attacking = true 
	var fulgorball = FULGORBALL.instance()

and then in the fulgorball script I did this

func _physics_process(delta: float) -> void:

func shoot_at_mouse(start_pos):
	self.global_position = start_pos
	var target_pos = get_global_mouse_position()
	var direction = (get_global_mouse_position() - start_pos).normalized()
	self.linear_velocity = direction * fulgor_ball_speed

if linear_velocity.x >= 1:
	$FulgorBall.position.x +=15
if linear_velocity.x <= -1:
	$FulgorBall.position.x +=15

So instead of looking for a specific position I just spawn the ball 15 pixels away from the player.
What do you think about this approach?

I’d like to know if it could bring issues in the future?

Zeetoh | 2020-06-08 06:34

it’s a nice approach, I mean. For starters setting up 2 position2d nodes to choose from was a bit whacky, but could have other uses maybe you wanted for your game, maybe having two auras with the hands moving with the player. The only comment I have regarding your code is that I don’t think the Area2D node moves with setting its linear_velocity. Don’t know if it has that property. If you want to use that methods you’d be better of with a KinematicBody2D. Good luck!

jtarallo | 2020-06-08 13:43