Godot Version
4.7.1
Question
How can I make a RigidBody2D or a CharacterBody2D draggable by the player, but still rotate based on gravity?
Currently this is the code I have for dragging a RigidBody2D with a rectangular collision area, and with an Area2D child (for detecting clicks):
var button_clicked := false
var previous_mouse_position := Vector2(0.0, 0.0)
func _on_area_2d_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
var global_mouse_position := get_global_mouse_position()
if event is InputEventMouseButton:
button_clicked = event.pressed
if event.pressed:
previous_mouse_position = global_mouse_position
func _physics_process(_delta: float) -> void:
var global_mouse_position := get_global_mouse_position()
if button_clicked:
position += global_mouse_position - previous_mouse_position
previous_mouse_position = global_mouse_position
I can’t figure out a way to even get close to my goal.
How do I lock linear movement but not rotation?
How do I make the node rotate based on gravity and on the local mouse position (which would be the pivot)?
How do I keep the point (on the node) on which my mouse is clicking in the same global position, even though the rotation of the node should be constantly changing?