- Home /
Picking up a box
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
Answer by Berenger · Jun 24, 2012 at 02:30 PM
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[i].transform.position).sqrMagnitude;
if( newDist < dist )
{
carriedObject = pickups[i].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
}
}
}
i will try my best and let you know .. thank you for answering.
I tried to follow this but i guess too little experience on my side, i ended up lost >.<
okay, this is clear now but going back to my original question, i think you miss understood it, i need the player to pick up a box and move around while it is in his hands not in a bag.. i appreciate your effort in helping me
Oh yep, I did misunderstand ! The idea is pretty much the same though, but you don't need a list as you can carry only one item at a time (I guess). And don't deactivate it, just remove the rigidbody if there is one. The tricky part will be the position between the hands. Is there a specific animation ?
Answer by sameer · Jun 24, 2012 at 11:45 AM
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
}
}
Hello sameer, i don't get what this script is trying to do, i tested it and nothing happens, can you please expand on it ? thank you
Hello, I'm trying to figure out why the offset is not working properly. As I set the z vector to 1, the object is suppose to be in front of the character, ins$$anonymous$$d it moves 1 forward on the world z-axis from the character is standing. Why is that?