Set position of a Rigidbody with a mouse offset

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dahaarl
:warning: Old Version Published before Godot 3 was released.

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)

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

razah | 2018-02-10 18:54