0 votes

Hey !

I'm messing with some Rigidbodies this week and trying to set an object position on x with the mouse and an offset where the click is done.

With the last KidsCanCode's video about Rigibodies, I understand that I should use _integrate_forces(state) instead of _physics_process but I still can't do what I want.

My problem is that the object follows the mouse click with the offset but it switch between positive and negative value (from the mouse I think) making my object glitching !

So here's my code to put on a Rigidbody2D:

extends RigidBody2D

func _integrate_forces(state):
    var mouse = get_global_mouse_position()
    var object_position = state.get_transform()

    # GET VECTOR BETWEEN MOUSE AND OBJECT
    var offset =  object_position.origin - mouse
    # ADD THE OFFSET
    object_position.origin.x = (mouse.x - offset.x)


    if Input.is_action_pressed("mouse_click"):
        state.set_transform(object_position)
in Engine by (30 points)

can you upload demo project or gif animation of your problem.

Please log in or register to answer this question.

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.