Hello
i need my character to pick up a box when i press say “b” and drop it when i press b again, i would really appreciate it if someone can give me an idea how to do that, it is 2D and i use C#
Thank you
Usually there is several unknown objects you can pickup, so the distance is a good way to know which one is going to be picked up. You can use Physics.OverlapSphere for that (the pickup needs a collider). Follow those steps :
- The player press the pickup button.
- You need to know what pickup is around => Physics.OverlapSphere. Those pickups must have a collider, and it would help if they had a specific layer to use with the function.
- This returns an array. Initialiaze a float to Mathf.Infity, go through the array and check the sqrMagnitude of (player.position - pickup.position). The lowest is the closest pickup, the one we are interested in, I’ll call it P.
- Set P child of the player, reset localPosition and localRotation, call P.gameObject.SetAciveRecursively(false).
- In your player script, declare an ArrayList and call it bag. Then, after the last point, bag.Add(P).
- If the player press the drop button, get the first or last element of the arraylist (best case scenario, there is a GUI to choose which object to drop, but let’s keep it simple) then remove it from the list. Activate it, unparent it and done !
If you have a problem, tell me at which point.
Here is a code to pick up the box and hold it in front of you. You’ll need an animation to have the hands at the right place though. This is untested and might not compile.
using UnityEngine;
public class Player : MonoBehaviour
{
// An object need to closer than that distance to be picked up.
public float pickUpDist = 1f;
private Transform carriedObject = null;
private int pickupLayer = 1 << LayerMask.NameToLayer( "Pickup" );
private void Update()
{
if( Input.GetButton( "pickup" ) ) // Define it in the input manager
{
if( carriedObject != null ) // We're holding something already, we drop
Drop();
else // Nothing in hand, we check if something is around and pick it up.
PickUp();
}
}
private void Drop()
{
carriedObject.parent = null; // Unparenting
carriedObject.gameObject.AddComponent( typeof(Rigidbody) ); // Gravity and co
carriedObject = null; // Hands are free again
}
private void PickUp()
{
// Collect every pickups around. Make sure they have a collider and the layer Pickup
Collider[] pickups = Physics.OverlapSphere( transform.position, pickUpDist, pickupLayer );
// Find the closest
float dist = Mathf.Infity;
for( int i = 0; i < pickups.Length; i++ )
{
float newDist = (transform.position - pickups*.transform.position).sqrMagnitude;*
if( newDist < dist )
{
carriedObject = pickups*.transform;*
dist = newDist;
}
}
if( carriedObject != null ) // Check if we found something
{
// Set the box in front of character
Destroy( carriedObject.rigidbody );
carriedObject.parent = transform;
carriedObject.localPosition = new Vector3( 0, 1f, 1f ); // Might need to change that
}
}
}
if u want to take it then delete then like hp or somthing like that so
bool deleted = true;
if(Input.GetKey("b")){
//delete the box Or Wear It
deleted = true;
}
if(Input.GetKey("b")){
if(deleted)
{
//DropIt Or UnWearIt
}
}