+1 vote

Im new to godot, i try this and its working,

onready var pos = position

func _process(delta):
    move()

func move():
    print(pos)
    pos += Vector2(10,0)
    set_position(pos)

so i create _input and place move function, but it cant move by any input.

func _input(event):
    print(event)
    move()

print(event) show on output, and print(pos) from move function also increments every input, but the object still not move. so why?

asked Jun 6, 2019 in Engine by bxgdo (13 points)

3 Answers

0 votes

It's recommended to use apply_central_impulse() and you should call it from _physics_process with an Input.is_action_pressed check:

func _physics_process(delta):
    move()

func move():
    if Input.is_action_pressed('ui_right'):
        apply_central_impulse(Vector2(10, 0))
answered Jun 6, 2019 by Dlean Jeans (3,915 points)

This is incorrect. Impulses are for instantaneous forces, and should not be applied every frame. For continuous force, use add_force() - see below.

Ah yeah, in _integrate_forces() not in _physics_process(). I recall it works the same but might hurt performance.

+2 votes

A rigid body cannot be moved directly, because it is controlled by the physics simulation. You move it by applying forces, not by changing its position.

In addition, any changes to the body's physics state must be done in _integrate_forces(), not in _physics_process().

You can see an example here:

Physics Introduction: Using RigidBody2D

See the RigidBody2D documentation for details.

answered Jun 6, 2019 by kidscancode (17,776 points)

Thanks for the explanation,
i think for simple movement, object should use area2d or kinematicbody node.
Oh and, i follow your tutorials on youtube. Great work.

–1 vote

Use set_axis_velocty().
Example

var WALK_SPEED = 100

    func _physical_process(delay):
if Input.is_action_pressed("ui_right") :
set_axis_velocity(Vector2(WALK_SPEED,0))
answered Jun 11 by Zedonboy (12 points)
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.