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)
asked Jan 2, 2018 in Projects by Dahaarl (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.