Pickup Item and Place Item System

Finally, I found an advanced way to make a Pickup Item and Drop Item System. As an Indie developer, I will share my experience

1. You need to create a Game Object in your character’s camera and name it (hand).
2. Create 2 scripts 1. Script with the name (PickupableItem) 2. Script with the name (PlayerController)
3. Go to the script (PlayerController) and write this script:
```csharp
**using UnityEngine:

public class PlayerController : MonoBehaviour
{
public KeyCode pickupKey = KeyCode.T; // Key for picking up an item
public KeyCode throwKey = KeyCode.E; // Key for throwing an item
public GameObject hand; // An object representing the location where the player holds items
public float reachDistance = 4f; // Interaction distance

private PickupableItem currentItem; // Link to the current item being picked up

void Update()
{
    if (Input.GetKeyDown(pickupKey))
    {
        TryPickupItem();
    }

    if (Input.GetKeyDown(throwKey))
    {
        if (currentItem != null)
        {
            // If we have an item, throw it away
            currentItem.Throw();
            currentItem = null; // We will reset the link because the item is no longer in hand
        }
    }
}

void TryPickupItem()
{
    // Check if the item is already holding
    if (currentItem != null)
    {
        Debug.Log("You already have the item in your hands.");
        return;
    }

    // Checking if there is a hand object
    if (hand == null)
    {
        Debug.LogError("Hand object is not assigned.");
        return;
    }

    // Create a beam forward from the player
    Ray ray = new Ray(transform.position, transform.forward);
    RaycastHit hit;

    // Checking if the beam hits something
    if (Physics.Raycast(ray, out hit, reachDistance))
    {
        // Checking if an object has a PickupableItem component
        var item = hit.collider.GetComponent<PickupableItem>();
        if (item != null)
        {
            // Calling the method for picking up an object
            item.PickUp(hand.transform);
            currentItem = item;
        }
    }
}

}**
** __**4. Go to the script (PickupableItem) and write this script:**__ **csharp
**using UnityEngine;

public class PickupableItem : MonoBehaviour
{
private Rigidbody rb;

private void Start()
{
    // Getting the component Rigidbody
    rb = GetComponent<Rigidbody>();

    // Making sure that Rigidbody and Gravity are configured correctly initially
    SetRigidbodyState(true);
    EnableGravity(true);
}

public void PickUp(Transform destination)
{
    // Teleport the object to the specified coordinates (hand coordinates)
    transform.SetParent(destination);
    transform.localPosition = Vector3.zero;
    transform.localRotation = Quaternion.identity;

    // Turn off Rigidbody and Gravity, since the item is now held by the player
    SetRigidbodyState(true);
    EnableGravity(false);
}

public void Throw()
{
    // Detach the item from your hand and add forward force to it for the throwing effect
    transform.SetParent(null);

    // Turn on Rigidbody and Gravity again for a correct fall
    SetRigidbodyState(false);
    EnableGravity(true);

    // Adding forward force for a throwing effect
    rb.AddForce(transform.forward * 3f, ForceMode.Impulse);
}

private void SetRigidbodyState(bool state)
{
    if (rb != null)
    {
        rb.isKinematic = state;
        rb.useGravity = !state; // Turn on Gravity if the item does not hold
        rb.collisionDetectionMode = state ? CollisionDetectionMode.Continuous : CollisionDetectionMode.Discrete;
    }
}

private void EnableGravity(bool state)
{
    if (rb != null)
    {
        rb.useGravity = state;
    }
}

}**
```
5. After you have created the scripts, go to your character’s camera and throw the script there: (PlayerController) after that, go to any of your items with which you want to interact and throw the script there: (PickupableItem)
6. It is necessary that your item has (Rigidbody) and Collider with Is Trigger Otherwise, it won’t work.
7. Also in your Player there must be a tag “Player” and so that it is present (Capsule Collider and also there must be an is Trigger)
8. Now you have a complete Pickup Item and Drop system. Enjoy

Just make the Item’s GameObject the child of the Player hand’s GameObject. There’s a ton of tutorials on Google/YouTube, or you can ask ChatGPT, for something this simple it’ll give a good answer.

Yeah, I know. I would like to know what script I should write for this, I already wrote this request in ChatGPT, but it makes my questions too complicated, and I’m still a noob at codes and it wouldn’t be bad if I knew what code to use in order to to pick up an item and place the item in the specified location.

I want to make a system for picking up an item and putting the item in the specified place like in this game Link - The Horror Within by Pollero

Well I don’t know the structure of your game, so I can’t give you a direct reply. When do you want your player to hold the item? when it collides with it (pickup from the world)? select it from the inventory? press a key? Showing your code will help.

So when you decide WHEN your player character should have the item in its hand, you just set the item parent to the player hand, with one line of code:

item.transform.parent = player.transform;

I will be glad to your help :slight_smile:

Can you post a video of the exact moment? I won’t play a full game just to see what you want :smile:

This could help:

Ok I’ll do it now :slight_smile:

I’ve already watched this video, it’s useful, but it’s not quite what I wanted.

OK then show us your player character code/script, and a video/picture of what you want to achieve, and I’ll try to help you.

Here is what I want to achieve
https://drive.google.com/file/d/1ijcOskjrrVGLf4CtfQ2clJxYVIun_IUP/view?usp=sharing


Here is my player code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public float movespeed;
    public float sensitivity;
    public float gravity;
    private float mouseX;
    private float mouseY;
    private float vertical;
    private float horizontal;

    public Vector2 clampangle;
    private Vector3 Velocity;
    private Vector2 angle;
    public Transform cameraTransform;

    private CharacterController charactercontroller;

    private Animator animator;

    private void Start()
    {
        charactercontroller = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;

    }

    private void Update()
    {
        vertical = Input.GetAxis("Vertical");
        horizontal = Input.GetAxis("Horizontal");

        Vector3 playerMovementInput = new Vector3(horizontal, 0.0f, vertical);
        Vector3 moveVector = transform.TransformDirection(playerMovementInput);

        if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("Vertical") < 0 || Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
        {
            if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Vertical") > 0)
            {
                movespeed = 4;
                animator.SetInteger("penis", 2);
            }
            else
            {
                movespeed = 2f;
                animator.SetInteger("penis", 1);
            }
        }
        else
        {
            animator.SetInteger("penis", 0);
        }

        if (charactercontroller.isGrounded)
        {
            Velocity.y = -1f;
        }
        else
        {
            Velocity.y -= gravity * Time.deltaTime;
        }

        charactercontroller.Move(moveVector * movespeed * Time.deltaTime);
        charactercontroller.Move(Velocity * Time.deltaTime);

        mouseX = Input.GetAxis("Mouse X");
        mouseY = Input.GetAxis("Mouse Y");

        angle.x -= mouseY * sensitivity;
        angle.y -= mouseX * -sensitivity;

        angle.x = Mathf.Clamp(angle.x, -clampangle.x, clampangle.y);

        Quaternion rotation = Quaternion.Euler(angle.x, angle.y, 0.0f);
        Quaternion rotationTwo = Quaternion.Euler(0.0f, angle.y, 0.0f);
        transform.rotation = rotationTwo;
        cameraTransform.rotation = rotation;
    }

Damn this is not that video, wait

Already updated Video https://drive.google.com/file/d/1ijcOskjrrVGLf4CtfQ2clJxYVIun_IUP/view?usp=sharing

I will be very grateful to you :slight_smile:

:eyes:

Anyway :smile:

What you’re trying to achieve is a little bit advanced to be explained in a forum post. You need to divide it into 6 steps:

  1. Detect when you’re close to a pickable item.
  2. Display text
  3. When player presses a button, pick up the item
  4. Detect when the player is holding an item AND is close to an object he can drop it into (table, bowl…)
  5. Display text
  6. When the player presses a button, drop the item.

For step 1-2, CodeMonkey has an excellent video about this:

https://www.youtube.com/watch?v=LdoImzaY6M4

Then for step 3, the first CodeMonkey video I posted deals exactly with this.

Steps 4-6 are very similar to 1-3, just with some conditions you need to check.

Try to remake CodeMonkey’s tutorials I posted on a new, clean project. Once you get it to work, try to integrate it into you project. If you need more help let me know.

This video is still not quite what I wanted :frowning:

It is exactly what you want.

Ok Let’s go in order, how can I pick up an item like in this video - https://drive.google.com/file/d/1ijcOskjrrVGLf4CtfQ2clJxYVIun_IUP/view?usp=sharing
The item should not be in the middle of the screen when interacting. Since you showed me in this video -
https://www.youtube.com/watch?v=2IhzPTS4av4

The object should hit only from the right corner like in the video - https://drive.google.com/file/d/1ijcOskjrrVGLf4CtfQ2clJxYVIun_IUP/view?usp=sharing

What script should I use for this?

I need to make a script in which when I interact with an object, it is deleted, but appears in my hands.