0 votes

So, I created a gun which reacts to my right stick :

extends Node2D

var shot = preload( "res://tscn/weapons/shot.tscn" )
var RS_x = 0
var RS_y = 0

func _ready():
    set_fixed_process( true )

func _fixed_process( delta ):

    RS_x = Input.get_joy_axis(0, 2)
    RS_y = Input.get_joy_axis(0, 3)

    if RS_x > 0.5 || RS_x < - 0.5 || RS_y > 0.5 || RS_y < - 0.5:
            _shoot_bullet( RS_x, RS_y )

func _shoot_bullet( RS_x, RS_y ):
    var new_shot = shot.instance()
    new_shot.set_global_pos( get_node( "shootfrom" ).get_global_pos() )
    new_shot._set_direction( Vector2( RS_x, RS_y ) )
    get_node("../..").add_child( new_shot )

It adds a shot, passing the stick direction as argument to this function (inside the "shot" node) :

func _set_direction( vector ):
    direction = vector
    direction = direction.normalized()
    direction_set = true

Everything works fine, and once inside the tree, the shot goes to the same direction until it's freed. That's what I want.

I then created an enemy, JackBot, which locates the player every frame and shoots him when he can :

extends Area2D

var shot = preload( "res://tscn/weapons/enemy_shot.tscn" )

var shoot_decay = 0.2
var time_since_shoot = 0
var shoot_direction

func _ready():
    set_fixed_process( true )

func _fixed_process(delta):


    if time_since_shoot < shoot_decay:
        time_since_shoot += delta
        time_since_shoot = 0
        _shoot_bullet( shoot_direction )

func _locate_player():
    shoot_direction = - ( get_node("shootfrom").get_global_pos() - get_node("../../player/target").get_global_pos() )

func _shoot_bullet( shoot_direction ):
    var new_shot = shot.instance()
    new_shot.set_global_pos( get_node( "shootfrom" ).get_global_pos() )
    new_shot._set_direction( shoot_direction )
    get_node("../..").add_child( new_shot )

I copied the _set_direction(): function from shot to enemy_shot (the node JackBot adds when he shoots). But instead of going straight in a line, the shot now follows the player, as if the vector argument was a pointer to the constantly updated shoot_direction variable, which tracks the position of the player.

So here are my two questions :
How do I copy the shoot_direction Vector2; so once it's passed to the enemy_shot, it remains the same Vector2 (and doesn't point anymore to the player's location) ?
And most important, why the shot instance doesn't change when I move the stick after it has been fired ?

Thanks a lot, please tell me if I did wrong formatting as it's my first question.

in Engine by (15 points)

Are you sure your shot is getting parented where you expect?

Wow, you're totally right. My code works this way : main/level/jackbot/enemy_shot. I parented the enemy_shot to main instead of level, which is the node that gets moved when I move the left stick (the Player node remains at the center of the screen).
So if the shot always tracked the player, it's not because of the _locate_player function keeping updating, it's because the offset of the moving level wasn't applied to it...
Thank you :) .

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.