- Home /
Can someone help me with a special pick-up code?
Hello! I'm new to unity and I need a script to carry an object with my third person character and find this script very helpful:
But I don't know which object or component I need to assign to the transform guide. And also I think the tempParent is my player's rigidbody, but I'm not sure.. I hope someone can help me. Thanks in advance!
Answer by jimmycrazyskills · Dec 22, 2018 at 05:22 PM
Hi mate, I had a little look at the script you linked:
The 'guide' gameObject seems to be the transform where the item will get moved to.
The 'tempParent' gameObject seems to be the object that will be the item's new parent
Example:
If a picked up a sword, the 'guide' gameobject might be a holder on the player's back and the 'tempParent' could be the holder gameObject or the main player's gameobject (depending on how you want the picked up item to move/rotate relative to the player)
Hoped this helped abit =D
Hello jimmycrazyskills :)! Thanks for your fast and kind help! I've tried it, but now my character gets stuck to the pickable object... It's a mystery to me... $$anonymous$$aybe I can describe you my outliner: I have a Player$$anonymous$$ovement Rigidbody with the movescript ans pickup script (parent), then a character Rigidbody (child; a 3d model).
Adhering to your instructions I have created an empty object as transform guide (I want the object to carry in front of the stomach and put it there) as child to the Player$$anonymous$$ovement. And I have assigned the whole 3d $$anonymous$$odel of the character as the tempParent. (If I take the character's hand as transform guide, the game crashes).
OUTLINER:
Player$$anonymous$$ovement (Rigidbody; with movement and pickup-script) = PARENT
EmptyObject as transform guide = CHILD 1
3d $$anonymous$$odel as tempParent = CHILD 2
Is this correct? Or does this cause the sticking problem? Would be very nice, if you can help me again :). Thanks and kind regards!
Hey mate sorry for the slow reply I was out! , WE WILL GET THIS WOR$$anonymous$$ING ;)
Could you paste your whole script, as it might be easier to find the problem?
If you can't paste it, do you know how/where item is being assigned?
Cheers :)
UPDATE Hi again, I've just created this myself and it appears to work ok here's what I did:
Hierarchy:
(Parent) Player GameObject > (Child) Empty ItemHolder GameObject
(Seperate in scene) A cube with an attached Rigidbody
The script component is attached to the main Player GameObject.
The Item
variable in the 'Inspector' panel is set to the cube in the scene.
The Temp Parent
and Guide
variables in the 'Inspector' panel are set to the ItemHolder GameObject
Hope this sheds some light on anything that might be different, if it still doesn't work give us a shout/message :)
Hello and thanks again! :) I would be very glad and thankful, if we get this to work :D! I've applied the script to the carry-object, but the carriedObject in the code is also the carry-object, is this ok? Now I can pick it up, but just if the transform guide is the hand of the character and then I just get the outliner of the cube, the mesh lies still on the floor :D. If I apply it to another body part or even if I make an invisble child of the player, the player gets stuck again..
I post the script, but I think it's the same from the link. $$anonymous$$y cube to pick up is a rigidbody with a collider. $$anonymous$$y players parent with the move-script is also a rigidbody with a mesh collider.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PickUpObject : $$anonymous$$onoBehaviour { bool carrying; public GameObject carriedObject; public GameObject tempParent; public Transform guide; public float range = 5;
// Use this for initialization
void Start()
{
carriedObject.GetComponent<Rigidbody>().useGravity = true;
}
// Update is called once per frame
void Update()
{
if (carrying == false)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && (guide.transform.position - transform.position).sqr$$anonymous$$agnitude < range * range)
{
pickup();
carrying = true;
}
}
else if (carrying == true)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
drop();
carrying = false;
}
}
}
void pickup()
{
carriedObject.GetComponent<Rigidbody>().useGravity = false;
carriedObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = true;
carriedObject.transform.position = guide.transform.position;
carriedObject.transform.rotation = guide.transform.rotation;
carriedObject.transform.parent = tempParent.transform;
}
void drop()
{
carriedObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = false;
carriedObject.transform.parent = null;
carriedObject.transform.position = guide.transform.position;
}
}
Thanks again!!!!! :)
UPDATE Hi again I just replied to your old message so I've re-pasted this, I've just created this myself and it appears to work ok here's what I did:
Hierarchy:
(Parent) Player GameObject > (Child) Empty ItemHolder GameObject
(Seperate in scene) A cube with an attached Rigidbody
The script component is attached to the main Player GameObject.
The Item
variable in the 'Inspector' panel is set to the cube in the scene.
The Temp Parent
and Guide
variables in the 'Inspector' panel are set to the ItemHolder GameObject
Hope this sheds some light on anything that might be different, if it still doesn't work give us a shout/message :)
Hi and thanks again! :) I think, there is a problem with the character itself, because if I test your method with a new scene, it works, with my scene, the character takes the object, gets stuck and when he throws the cube, it first goes up about one meter and then it falls on earth. $$anonymous$$y character is child of an empty rigidbody with the movement-script. The character itself isn't a rigidbody and consists of different components. But I can't change that anymore.. $$anonymous$$aybe you have an idea :), or I have to change the hole thing somehow. Definitely I want to thank you for your time and help!! :)
Hey again, From what you're saying I'm wondering if it's a collider problem, your movement might be stopping if your Player collider is inside/overlapping with the picked up object.
As a test try moving the Holder Transform quite far away from your Player just to make sure no collisions are happening.
If this still doesn't work give us a shout again or if you can somehow export your project that way I could look at exactly what's happening a bit easier :D
IT WOR$$anonymous$$S!!!! :D Yes, it was the collider, now it works just fine :)! Thanks SO $$anonymous$$UCH for your help!!! I hope you have a nice sunday and thanks also for your time, I really appreciate it!! :)