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.
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:
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.