Search Unity

How to pick up itens?

Discussion in 'Scripting' started by gilson, Dec 22, 2010.

  1. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
    Hello, i hope that someone can give a hand of how to do it.

    I'm using a character controller as a player, but since i'm thinking about to switch to a rigidbody with "isKinematic" flag on, and turns it off when i need physics to run on my player. What i'm traying to do right now is to make the player pick up a item and hold in his hand. So i think that i could add a hinge to the player rigidbody dynamically. Is it possible?

    Do you guys have any sugestions of how to do this?
     
  2. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    748
    Make the character look like they are picking up the object? Or actually pickup an object after running over it?

    Make it look like it's picking it up...
    1) Animate it in your 3D program and call the animation
    2) If your character is made up of parts animate it using the Unity animation tool
    3) If your character is made up of parts animate it using script

    Picking it up.
    Add a box collider to your object and then add a script to your character controller

    Code (csharp):
    1.  
    2. void OnTriggerEnter(Collider collider)
    3. {
    4.  
    5. [INDENT]    if (collider.tag == "Gun")
    6.    {
    7. [INDENT]        givePlayerGun();
    8. [/INDENT]    }
    9. [/INDENT]
    10. }
     
  3. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
    The thing is, how do i add the object to the player? Im working only with the script part, with no animation or anything. My player is a ball and the item is a cube. I want the sphere to hold the cube. Like, attaching to it. How would i "givePlayerGun();" in my situation?
     
  4. PhYc1aTr1st

    PhYc1aTr1st

    Joined:
    Sep 14, 2010
    Posts:
    19
    maybe you could state the position you want the cube to be, like right in front of the sphere. or you could create an empty object, once the sphere collides with the cube, the cube moves to that empty objects position. while the empty object should be a child of the sphere, therefore following it.
     
  5. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    748
    Try something like this:

    Code (csharp):
    1. cube.transform.parent = sphere.transform
    2.  
    3. sphere.transform.localPosition = new Vector3(1,1,1);