how to move an instanced scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ddarkmoto
:warning: Old Version Published before Godot 3 was released.

Hi everyone. I’m trying to move an instanced scene using Kinematicbody2d’s move() method but it doesn’t seem to work. I mean it worked, but not the way I expected. What I wanted for it to happen was for the projectile to continuously move but it doesn’t. A Piece of my code is shown below. Thanks to all who could answer and I’d really appreciate it.

		var prjrelease = projectile.instance()
	position = get_node("/root/world/character").get_pos() + get_node("/root/world/character/Position2D").get_pos()
	prjrelease.set_pos(Vector2(position.x , position.y))
	get_parent().add_child(prjrelease)
	
	prjrelease.move_to(Vector2(position.x * delta, 0))

Can you tell me what your script does presently?

not the way I expected

It helps members here to help you.

Anutrix | 2017-04-06 04:38

The code above instantiates a projectile scene and then it’s supposed to move every frame. But it doesn’t.

ddarkmoto | 2017-04-06 13:30

:bust_in_silhouette: Reply From: CowThing

move_to() will instantly move the KinematicBody2D to that position. What you want to do is make a script for the bullet that will move it every frame in the _fixed_process() function.

Here’s a quick example:

extends KinematicBody2D

var SPEED = 100

var shoot_dir = Vector2()

func fire(position, direction):
	shoot_dir = direction.normalized()
	set_pos(position)
	
	set_fixed_process(true)

func _fixed_process(delta):
	move(shoot_dir * SPEED * delta)
	# Can do other things if nessicary, like checking if the bullet is_colliding()

Calling the fire() function will fire the bullet from position in the direction you pass it.

var bullet = bullet_scene.instance()
add_child(bullet)
bullet.fire(gun.get_pos(), Vector2(1, 0))

So you’d do something like that when spawning the bullet.

Excellent! This is what I need. Thank you very much CowThing! Cheers mate

ddarkmoto | 2017-04-06 08:00