Pick up item physics

Godot Version

4.3

Question

Hi! I ran into a problem: an item that the player picks up is rolling and dance to much. I want the object not to move at all in his hands, but I still haven’t figured out how to do it, please help!
here is the item code:

extends RigidBody2D

var picked = false

func _physics_process(delta: float):
if picked == true:
self.position = get_node(“…/player/Marker2D”).global_position

func _input(event: InputEvent):
if Input. is_action_just_pressed(“ui_pick”):
var bodies = $Area2D.get_overlapping_bodies()
for body in bodies:
if body.name == “player” and get_node(“…/player”). canPick == true:
picked = true
get_node(“…/player”). canPick = false
if Input.is_action_just_pressed(“ui_drop”) and picked == true:
picked = false
get_node(“…/player”).canPick = true
apply_impulse(Vector2(), Vector2(5,-5))
else:
apply_impulse(Vector2(), Vector2(-5,-5))

here is the player’s code (if its needs too):

extends CharacterBody2D

var canPick = true
@export var speed = 200.0
@export var jump_height = -400.0

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(delta):

if not is_on_floor():
velocity.y+= gravity * delta

if Input.is_action_just_pressed(“ui_accept”) and is_on_floor():
velocity.y = jump_height

var direction = Input.get_axis(“ui_left”, “ui_right”)
if direction:
velocity.x = direction * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)

Thank you so much!!

Edit: Sorry, the below is for 3D, but RigidBody2D also has a boolean to lock rotation, lock_rotation, and an angular_damp float.


There are options for Angular Damp that might work. It could be set high when the player picks up the item, and low when they drop it.

There’s also a property for RigidBodies called Axis Lock that keeps them from rotating on a particular axis, which could be toggled similarly.

Both kinds of properties are accessible from the editor as well.

1 Like

Use RigidBody3D.freeze = true when its picked, and set it to false when on the ground.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.