I’m implementing a mechanic in my game where the player can hold an object. Currently, the player is a CharacterBody2D and the player is a RigidBody2D. My idea was when the object is picked up, I attempt to disable physics until its released. The object’s position when held is mapped to a Marker2D within the player when it becomes a child of the player. Currently, this is the code I have within the object’s script:
Unfortunately when testing the code though, the object doesn’t stay in place at the point. It’s still affected by game physics, being pulled down by gravity, but the object is still part of the player (when picked up) and seemingly remains in the spot of the marker, but as if its being dragged.
How could I fix this? You can see my attempts to freeze the object in the equip function.
You’re not setting isBeingHeld to true in your equip function, I don’t know if that affects something in some other part of your code.
Also we don’t see where hold() is being called. When you parent the object to the character it will automatically move with the partent, being “dragged” might be just because of that, and not your hold function. I’d print something in your hold function to see if it’s getting called roperly.
I realized that hold isn’t being called from your comment. Thanks. This is now what I have implemented in my process function:
func _process(delta: float) -> void:
if isBeingHeld:
hold(myParent.holdPoint)
This is honestly fine, but now, the object has no collisions. Can I possibly turn collisions back on? I’m going to read the docs for this, if you can reply fast enough to help, thanks, and sorry for the late reply.
From what im reading, I set freeze_mode to kinematic, but to no avail. Although held in the right spot, it can still clip through walls when I get close
I never used FREEZE_MODE_KINEMATIC so don’t know for sure, but It might be that even though it’s colliding, you force it into the wall by setting it’s position “manually” every frame.
I would try parenting the object to the holdPoint and only setting it’s position to zero once, after you parented it. Setting the position to Vector2.ZERO should move the object to it’s parent (the HoldPoint), then it should follow it automatically (because it’s parented to it), and maybe it wouldn’t let it clip into walls. (So don’t call hold every frame, to set it’s position manually) But just an idea..