0 votes

i have some code to spawn a cube on the player when i press R, but whenever i do it just teleports the cube to where the player is instead of spawning a new one. i probably did this wrong, so some help would be apreciated. heres my code:

var cube = load("res://cube.tscn").instance()

func _process(delta):
if Input.is_action_just_pressed("R"):
    get_tree().root.add_child(cube)
    cube.global_transform = self.global_transform

im pretty new to this so sorry if this is a stupid question

Godot version 3.3.2
in Engine by (15 points)

1 Answer

+1 vote
Best answer
 var cube = load("res://cube.tscn") 
 var cube_node

 func _process(delta): if Input.is_action_just_pressed("R"):
     cube_node = cube.instance()
     get_tree().root.add_child(cube_node)
     cube_node.global_transform = self.global_transform

Try something like this maybe ? I'm myself new too, but I believe that instancing your cube directly is what makes it keep the same "reference", kind of.
Instancing a new one on each key press instead should work

by (51 points)
selected by

that worked! thank you so much.

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.