Picking up items and floating them in front of the character

I am having a bit of trouble with a project I am working on. I would like for the player to be able to pick up rigidbody items, but not put them in an inventory. Instead, I would like for the item to simply float in front of the character’s view, until they press the same key again (in this case, ‘e’) and the item will drop from whatever place the character last had it held at. This is similar to games like in the Half-Life series. Would I just reparent the target entity as a child of the player’s camera? And how would I go about that exactly?

Oh, and here is the project:

Hi @cyron!

I think that’s what I have done in my example project. You can check and fork the project if you want.


1 Like

Ok, that actually is mostly what I was going for, except for a couple things.
I’d like for it to be the case that you must be within a certain radius in order to pick up the item, and in your example, whenever an object is dropped, it seems to “snap” to a certain rotation, which also is not what I am going for. Is there a way for me to do these two things?

Because it is just a basic example project, you need to add something like this yourself.

You can probably fix this by storing the rotation before reparenting and apply this rotation after reparenting. Just like I did for the position.

OK, thanks!

OK, so there is one more problem.
The script I am using to pick up objects and drop them, allows for me to put them inside of the floor. I am new to this programming concept, and I am unsure how to proceed.

Yes, that’s as expected, because I set the rigidbody type to kinematic when you drag the object. I’m not sure if there is a way to keep the rigidbody dynamic when dragging.

I’ll do some trial and error and see what happens.

Maybe, instead of reparenting, you can teleport the object to a specific drag position as long as you drag, but I’m not sure if a collision will work as expected when you do this.

Yeah, I’m not sure the dragging would be as smooth either.

I have a better idea. You can stop the dragging after collision with another object. :partying_face:

OK, so I just tried dragging a dynamic body, it did not work.
So here is my idea right now:
Add a collision function to the script, so that whenever the dragged entity comes into contact with another entity, then it translates it up a little bit.

Oh, you just said that.

So now it’s telling me that “collison” is undefined when I try to set up the event listener.

Is there a collision component on the entity with the script?

No, I have the player’s camera as the entity with the script.
I actually put this.pickedEntity instead of this.entity as the target, although it is during the initialize function, when this.pickedEntity is null.

Can you please try to add it where this.pickedEntity is not null anymore?

I’m not sure how I would do that.
this.pickedEntity is the target entity that you are picking up, so whenever the script boots up, it’s undefined anyway. If I completely remove the var pickedEntity = null; from the initialize() function, then the variable will be undeclared and the script will no longer function. Maybe I could assign it some random placeholder value, like a number or something?
EDIT: Nevermind, placeholder value does not change anything.

No, please do not remove anything.

Please try to add the line below after a raycast result. So where this.pickedEntity is not null anymore.

this.pickedEntity.collision.on('collisionstart', this.onCollisionStart, this);