- Home /
Multiple technical questions/Design discussion
Picking up objects and dropping them
Hello. I want to add a functionality in my game to make the player able to pick up certain objects and dropping them.
The logic I want is that the player should move next to the object he wants picked up, then a GUI text shows up telling him "Press E to pickup/drop", and when he picks it up it should move infront of him while he is moving, and when he presses E again it should drop infront of him where he stopped (classic pickup/drop style.)
I am programming in C#, but I am missing the logic behind this. Can anyone please help? Pseudocode will be fine.
Look at what you wrote.
The logic I want is that the player should move next to the object he wants picked up
Check the distance, or use collision.
then a GUI text shows up telling him "Press E to pickup/drop
Show gui text if distance is close or collided.
when he picks it up it should move infront of him while he is moving
If picked up, object becomes owned-by/parented-to the character, and it's position is set to some defined position in front of the character.
and when he presses E again it should drop infront of him where he stopped
If the button gets pressed again, object is disowned/unparented and (assu$$anonymous$$g it is a rigidbody that uses gravity) it falls to the ground.
A question like this one that is more design discussion rather than solving a specific technical problem is not a good fit for Unity Answers. You have multiple technical questions here, with multiple different ways of solving them. There are also a number of posts on UA concerning pickup and drop. Research:
Input.Get$$anonymous$$eyDown() and Input.GetButtonDown() for getting the input.
OnCollisionStay() OnTriggerStay() and Vector3.Distance() for ways of detecting the object being near.
OnGUI for displaying messages.
Vector3.Lerp(), Vector3.$$anonymous$$oveTowards(), and making objects parents and children (Transform.parent) for picking something up.
If when you put together your script, you have difficulty what a specific part of your script, we welcome your new question.
Okay, I understand! Thank you for your pleasant feedback.