Hello,
I am new to Unity and I was wondering how I could make a character (in this case, a first person camera) pick up an object (by hitting spacebar). The character could also drop the object at a new location (for example, by hitting spacebar).
Any ideas? Any help is greatly appreciated!
Thanks!
the first thing you need to do is determine whether there’s anything to pick up. You can do this mathematically, but the easiest thing to do is to have a trigger collider directly in front of the player controller representing the area you can pick things up in. Your pickups should have either a particular script or a tag set so the pickerupper script knows that not everything with a rigidbody can be picked up.
Put a script on your picker-upper-trigger-collider that looks something like this:
var currentPickup : Rigidbody;
var holdingPosition : Transform; //a transform representing where the object will be held
function FixedUpdate() {
if (currentPickup) {
currentPickup.position = holdingPosition.position;
currentPickup.velocity = Vector3.zero; //without this, after holding it for a long time gravity would build up a huge amount of speed when you release
}
}
function OnTriggerStay(col : Collider) {
if (!currentPickup col.gameObject.tag == "pickups" Input.GetKeyDown("space")) {
currentPickup = col.attachedRigidbody;
}
}
(untested code; if it’s broken, fixing it is left as an exercise for the reader) 
unless you want to see you are carrying said object (like a box or something)
you probably want to destroy the object you picked up,
log somewhere that you are now carrying the object,
then instantiate a new one when you drop it.
if you want to see the object while it’s carried, try making it semi-transparent on pick up – easiest way is to assign a new material.
Check out Destroy, and Instantiate. Read up on those as a starting point.
Manta’s advice is great too
Brian,
I like your advice about destroying the object to pick it up and then instantiating a new one to put it down, but how would I log somewhere that I am carrying the object?