+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?

in Engine by (13 points)

3 Answers

–1 vote

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))
by (4,179 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.

+5 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.

by (21,537 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.

–2 votes

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))
by (10 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 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.