picking up an object on key press

hello,

im working on a game where there are 3 objects, each have to be picked up and taken to the correct marker then dropped on it. I would like to do this with a key press i.e walk up to object > press p key to pick it up > carry object to marker > press p key again to drop that object (or a different key if its easier)

i would like to visually see the box being carried or just the top of it (the game uses the FirstPersonController) and i do need to see it placed on the marker with a gui text on the screen that confirm it i.e number of objects delivered - 1/3 etc

can anyone help me get started on this,
thanks

Matt

I’m having a hard time getting my thoughts into words today so I’m just gonna write some pseudo code :slight_smile:

  • Add “carryable” to the tag manager.
  • Set the tag to “carryable” for the objects you want to pickup.

PickupBehaviour.js

on the character

var itemCurrentlyHeld : Transform;
var headCamera : Transform;
var pickupRange : float;
var carryOffset : Vector3; // position of a carried object relative to the camera

function Update () {
	if (itemCurrentlyHeld) {
		itemCurrentlyHeld.position = headCamera.position + carryOffset;
		itemCurrentlyHeld.rotation = headCamera.rotation;
	}
}

function Pickup () {
	if (itemCurrentlyHeld) { return; } // stop if we're already holding something
	var hit : RaycastHit;
	if (Physics.Raycast(headCamera.position, headCamera.forward, hit, pickupRange)) {
		if (hit.collider.CompareTag("carryable")) {
			itemCurrentlyHeld = hit.collider.transform;
		}
	}
}

function Drop () {
	if (!(boolean)itemCurrentlyHeld) { return; } // stop if we're not holding anything
	itemCurrentlyHeld = default(Transform);
}

Like i said… pseudo code, not tested. but the approach is sound.