Hi all,
wondering if you could help me out. I would like to have a character (3rd person) pick up an item upon walking over it, then having that item appear in the character’s right hand, and remain there. Like an instantaneous thing. Is that a simple thing to do? Linking the item to the characters hand bone? or is there more to it? i just don’t know where to start with it, as my scripting knowledge is limited.
If anyone has any tips, suggestions, it would be greatly appreciated!
You can detect the object to be picked up with the OnControllerColliderHit function on the character’s script. A good way to go is to give all such objects their own tag and then check the tag within the function:-
function OnControllerColliderHit(hitObj: ControllerColliderHit) {
if (hitObj.gameObject.tag == "Pickable") {
...
}
}
Having established that the object has been touched, you can attach it to the character’s hand by setting its transform.parent property to the hand transform. Bones are treated as child transforms of the skinned object, so you can use transform.Find to access it (you need to specify the “pathname” of the hand bone in the bone hierarchy).
Just plan one step of the logic at a time.(ie)
(1)Create a trigger on the item. A sphere collider(set to trigger) would work good. Make it rather small so that the player has to walk right over it. By calling on trigger enter you can set a condition to only let a payer(tagged player) trigger the code like.
var pickupS : AudioClip;
//==========================//
function OntriggerEnter(col : Collider) {
if(col.gameObject.tag=="Player") {
audio.PlayOneShot(pickupS);
}
}
(2) Now we want the object to relocate itself to the players hand. There are more ways to do this than you can shake a stick at. One popular method is to make the object a child of the players hand. Or make the object a child of an empty GameObject that is a child of the payers hand.(ie)
var playerH : Transform;
var pickupS : AudioClip;
//==========================//
function OntriggerEnter(col : Collider) {
if(col.gameObject.tag=="Player") {
audio.PlayOneShot(pickupS);
transform.parent = playerH.transform;//This transforms new parent is now the players hand transform.
transform.localPosition = Vector3(0, 0, 0);//Its new local position is centered on the parent and made dead center.
rigidbody.isKinematic = true;//IsKinematic locks it in place to only be moved by the parent.
}
}
(3) Drop it or destroy it. Again many ways to do this. To just drop the item thats equipped, send the command from the parent(Ie)
//new code on the parent transform
function Update() {
for (var child : Transform in transform) { //to talk to the child transform.
if (Input.GetButtonDown("Drop") { //Create Drop in the input manager.
child.rigidbody.isKinematic = false;//Release it so it can use physics again.
child.transform.parent = null;//UnParent it.
}
}
Most of this code is off the top of my head and untested, so if it doesnt work then it will atleast give you some good ideas of how to pichup an item from walking over it and reposition it to your players hands and drop it when your done.
It may work with just a couple of errors fixed.LOL:)