Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Picking up and holding an object

Discussion in 'Scripting' started by chhenderson, Feb 18, 2010.

  1. chhenderson

    chhenderson

    Joined:
    Feb 15, 2010
    Posts:
    10
    Ok I give up. The effect I want is for one object (player, 3rd person) to pick up and hold an object and then be able to drop it.

    I tried detecting if they are touching, and then when a button is pressed the object is parented to the player object. But I'm going nowhere fast. Any help is appreciated.
     
  2. PrvtHudson

    PrvtHudson

    Joined:
    Apr 10, 2009
    Posts:
    236
    Don't give up, you just got here ;) Welcome.

    Parenting the "pickup" to the "player" should work just fine.

    Are you having a problem triggering that code? Post it so folks can see what is going on.

    You could
    1. put a "Trigger" around the pickup. A Trigger is a cube that has the "Is Trigger" flag set.
    2. when the trigger is triggered, you get the information as to who (player) triggered it.
    http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html
    3. parent the pickup to the player
    http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html

    One thing to note : If you are expecting the pickup to fall to the floor, then you have to make it a rigidbody when you "drop" it.
    http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html
     
  3. chhenderson

    chhenderson

    Joined:
    Feb 15, 2010
    Posts:
    10
    My problem now is that I dont know how to reference other objects. I have this code below on the trigger but I dont know how to associate the player and the object. And the same goes for the parenting code.

    Code (csharp):
    1. function Update () {
    2. }
    3.  
    4. function OnTriggerStay(other : Collider){
    5. if(Input.GetButton("Fire1")){
    6.     Destroy(gameObject);
    7.     }
    8. }
    Thanks for the help btw.
     
  4. PrvtHudson

    PrvtHudson

    Joined:
    Apr 10, 2009
    Posts:
    236
    // For this example, this is a component on the pickup.
    // Declare player public so it shows up in the inspector.
    // Drag the player onto pickup/player, in the Inspector for the pickup.
    // You can now reference it all you want.

    Code (csharp):
    1.  
    2. public Transform player;
    3.  
    4. function Update ()
    5. {
    6. }
    7.  
    8. function OnTriggerStay(other : Collider)
    9. {
    10.    if(Input.GetButton("Fire1")){
    11.  
    12.       Destroy(gameObject);
    13.  
    14.    }
    15.  
    16.       player.SendMessage ("YouFiredATrigger", "pickup");
    17.  
    18. }
    19.  
    I have to point out that this example is a little funky because you could accomplish the same thing by referencing the "other" variable in the function. ( if (other.tag = "Player") ... )

    Here are some tuts to get you going.

    http://unity3d.com/support/documentation/video/

    http://forum.unity3d.com/viewtopic.php?t=28433
     
  5. chhenderson

    chhenderson

    Joined:
    Feb 15, 2010
    Posts:
    10
    Alright I'm almost there.

    I have a trigger cube parented to my object mesh (object > trigger) and I want to delete the entire instance (parent and child) when the cube is triggered.

    Thoughts?
     
  6. PrvtHudson

    PrvtHudson

    Joined:
    Apr 10, 2009
    Posts:
    236
    Hey

    There is whole bunch of different ways to accomplish that. Here's one.

    Code (csharp):
    1.  
    2. public class bump_delete : MonoBehaviour {
    3.  /*
    4.   * This attaches to trigger
    5.   * The trigger and the box are in the same directory.
    6.   * Here is an example of Walking into the trigger, picks up box.
    7.   * Walk back into trigger, it deletes the box.
    8.   *
    9.  */
    10.     public Transform box;
    11.     public bool hasBox = false;
    12.  
    13.     void  OnTriggerEnter(Collider player) {
    14.         if (!hasBox){
    15.             hasBox = true;
    16.             box.transform.parent = player.transform;
    17.         }else{
    18.             Destroy(box.gameObject, 5);
    19.         }
    20.     }
    21. }
     
  7. chhenderson

    chhenderson

    Joined:
    Feb 15, 2010
    Posts:
    10
    Thanks for the help, but I just cant seem to combine what you gave with what I have. Also Im getting a lot of errors with your code. Im using JavaScript, is that C#?

    Here's what I'm trying to accomplish. The player has a box parented to his chest that turns its renderer on and off. There is a cube object on the ground with a trigger parented to it. When the player enters the trigger space and presses "q", the cube on the ground is destroyed and the cube on the chest is rendered. Then when the player presses "e", the chest cube is derendered and a new cube is instantiated in its place.


    This is the script on the chest cube.
    Code (csharp):
    1.  
    2. var dropBox : Transform;
    3.  
    4. function Update () {
    5.  
    6. if(Input.GetKeyDown("q")){
    7. renderer.enabled=true;
    8. }
    9.  
    10. if(Input.GetKeyDown("e")){
    11.     renderer.enabled=false;
    12.     boxtemp = Instantiate(dropBox, transform.position, transform.rotation);
    13. }
    14. }


    This is the script on the trigger of the other cube. Right it just deletes itself, but I want it to delete the cube that it is parented to.
    Code (csharp):
    1.  
    2. function Update () {
    3. }
    4.  
    5. function OnTriggerStay(other:Collider){
    6. if(Input.GetKeyDown("q")){
    7.     Destroy(gameObject);
    8. }
    9. }
    10.  
    11.  
    12.  
     
  8. PrvtHudson

    PrvtHudson

    Joined:
    Apr 10, 2009
    Posts:
    236
    Yes I am C#.

    gameObject is the current object. Sounds like you want the parent gameObject. Try this.

    Code (csharp):
    1.  
    2. function OnTriggerStay(other:Collider){
    3. if(Input.GetKeyDown("q")){
    4.    Destroy(transform.parent.gameObject);
    5. }
    6.  
     
  9. chhenderson

    chhenderson

    Joined:
    Feb 15, 2010
    Posts:
    10
    Hot damn! Thats exactly what I needed.

    Code (csharp):
    1. (transform.parent.gameObject);
    Thanks a ton.
     
  10. PaulHarisson

    PaulHarisson

    Joined:
    Jan 31, 2011
    Posts:
    13
    i want to pick again the dropped object. HELP
     
  11. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    i did mine with raycast, and to move towards the raycast's hit point
    but it didnt worked well so i deleted it...
     
  12. obliviux

    obliviux

    Joined:
    Mar 17, 2011
    Posts:
    137
    When you pick it up, can't you set the rotation and position of the cube to be same as yours?

    and then when you drop it, stop calling the function so it just drops on the ground if gravity is enabled?
     
  13. exvalid

    exvalid

    Joined:
    Oct 20, 2017
    Posts:
    9
    SOLVED

    Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

    all you have to do is add the movements in you want to the ObjectReplyIdAndMove script and add some conditions for the overides in the Main player Script ObjectGrabIdAndMove if u want to move the object out of center view.. currently when object is selected its not told to move but is ready too.

    ObjectGrabIdAndMove goes on Main player,.. ObjectReplyIdAndMove goes on Moveable Objects rember to add layers in you want to hit
     

    Attached Files:

    Last edited: Oct 20, 2017
  14. exvalid

    exvalid

    Joined:
    Oct 20, 2017
    Posts:
    9
    Solved..


    Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

    all you have to do is add the movements in you want to the ObjectReplyIdAndMove script and add some conditions for the overides in the Main player Script ObjectGrabIdAndMove if u want to move the object out of center view.. currently when object is selected its not told to move but is ready too.

    ObjectGrabIdAndMove goes on Main player,.. ObjectReplyIdAndMove goes on Moveable Objects rember to add layers in you want to hit
     

    Attached Files:

  15. exvalid

    exvalid

    Joined:
    Oct 20, 2017
    Posts:
    9
    This raycast should work the way you wanted, in the scripts i uploaded, mayb did u mean to throw it? i was thinking for the call to get the ID
     
  16. exvalid

    exvalid

    Joined:
    Oct 20, 2017
    Posts:
    9
    ReCompiled Scripts Alomst Finished bar Diagnals and mouse rotation defualt is working with full inversion raycast linecast ID check Ect ect its all there Enjoy. ill be finishing the Mouse rotations over the week took lopng enough to get this far.
     

    Attached Files:

  17. myrenfrez

    myrenfrez

    Joined:
    Dec 5, 2017
    Posts:
    1
    Hi, any chance to do a step by step guide for a noobie like me? im trying to make this work for my project, but having trouble to make it work. :/
     
  18. chernandez118

    chernandez118

    Joined:
    Jun 14, 2023
    Posts:
    1
    I was trying to figure out my error, however I still cant figure out why my PickUP object wont convert or become the child of my Player. It wont become the object held so it just remains in the same place although I have pressed the button. Any suggestions to get it to work?
    Code (CSharp):
    1.  {
    2.         if(Input.GetKeyDown(KeyCode.E) && objectHeld == null && targetObject !=null)
    3.         {
    4.             objectHeld = targetObject;
    5.             objectHeld.transform.SetParent(transform);
    6.  
    7.             objectHeld.transform.localPosition = Vector3.zero;
    8.         }
    9.         else if(Input.GetKeyDown(KeyCode.E) && objectHeld != null)
    10.         {
    11.             objectHeld.transform.SetParent(null);
    12.             objectHeld = null;
    13.         }
    14.     }
    15.  
    16.     void OnTriggerEnter(Collider collider)
    17.     {
    18.         if (collider.gameObject.layer == 9 && targetObject == null) //target will be first object collided with
    19.         {
    20.             targetObject = collider.gameObject;
    21.         }
    22.     }
    23.     void OnTriggerExit(Collider collider)
    24.     {
    25.         if(collider.gameObject.layer == 9)
    26.         {
    27.             targetObject = null;
    28.         }
    29.     }
     
  19. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,367
    You should create a new forum thread if you have a problem instead of replying to an unrelated post.