The "invalid call" on PackedScene means you forgot to call instance()
. Calling instance
on a PackedScene gives you a new instance of that scene on which you can call stuff like get_pos()
if the root inherits Node2D.
"Gus": I don't see Gus written anywhere in this thread, maybe you meant "Gun"?
the variables I used in my code are supposed to exist in the context, I don't know your whole project but I can give you an example how to achieve things. It just means that, in order to make a shot move towards something, you need to know where it comes from (the gun) and where it should go (the target) and at which speed (speed). You can probably get these variables by using get_node
or having members defined in your scripts.
shot.set_linear_velocity
is meant to tell a physical node to move in a given direction at a specific speed (until it eventually hits something). I used this example assuming it could be a RigidBody, but if you want to calculate motion manually, here is how you can do on your Shot node:
# How many pixels the shot travels vertically and horizontally per second
var velocity = Vector2(0,0)
func _ready():
# Tell Godot to call _fixed_process at every physic frame
set_fixed_process(true)
# Call this function when the shot is created so you can set the speed
func set_linear_velocity(v):
velocity = v
func _fixed_process(delta):
# This is a basic movement formula with which the node will move in a straight line
set_pos(get_pos() + velocity * delta)