0 votes

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))
in Engine by (45 points)

Can you tell me what your script does presently?

not the way I expected

It helps members here to help you.

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

1 Answer

+1 vote

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.

by (675 points)

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

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 Frequently asked questions and 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 [email protected] with your username.