Picking up and holding an object

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.

Don’t give up, you just got here :wink: 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.
    Unity - Scripting API: Collider.OnTriggerEnter(Collider)
  3. parent the pickup to the player
    Unity - Scripting API: Transform.parent

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.

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.

function Update () {
}

function OnTriggerStay(other : Collider){
if(Input.GetButton("Fire1")){
	Destroy(gameObject);
	}
}

Thanks for the help btw.

// 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.

public Transform player;

function Update () 
{ 
} 

function OnTriggerStay(other : Collider)
{ 
   if(Input.GetButton("Fire1")){ 

      Destroy(gameObject); 

   } 

      player.SendMessage ("YouFiredATrigger", "pickup");

}

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

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?

Hey

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

public class bump_delete : MonoBehaviour {
 /*
  * This attaches to trigger
  * The trigger and the box are in the same directory.
  * Here is an example of Walking into the trigger, picks up box.
  * Walk back into trigger, it deletes the box.
  * 
 */
    public Transform box;
    public bool hasBox = false;

	void  OnTriggerEnter(Collider player) {
        if (!hasBox){
            hasBox = true;
            box.transform.parent = player.transform;
        }else{
            Destroy(box.gameObject, 5);
        }
	}
}

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.

var dropBox : Transform;

function Update () {

if(Input.GetKeyDown("q")){
renderer.enabled=true;
}

if(Input.GetKeyDown("e")){
	renderer.enabled=false;
	boxtemp = Instantiate(dropBox, transform.position, transform.rotation);
}
}

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.

function Update () {
}

function OnTriggerStay(other:Collider){
if(Input.GetKeyDown("q")){
	Destroy(gameObject);
}
}

Yes I am C#.

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

function OnTriggerStay(other:Collider){ 
if(Input.GetKeyDown("q")){ 
   Destroy(transform.parent.gameObject);
}

Hot damn! Thats exactly what I needed.

(transform.parent.gameObject);

Thanks a ton.

i want to pick again the dropped object. HELP

i did mine with raycast, and to move towards the raycast’s hit point
but it didnt worked well so i deleted it…

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?

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

3260749–251399–ObectGrabScripts.zip (4.12 KB)

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

3260751–251401–ObjectGrabIdAndLock.zip (2.63 KB)
3260751–251402–ObjectReplyIdAndMove.zip (1.51 KB)

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

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.

3285326–254256–ObjectGrabScripts.zip (10.9 KB)

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. :confused:

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?

 {
        if(Input.GetKeyDown(KeyCode.E) && objectHeld == null && targetObject !=null)
        {
            objectHeld = targetObject;
            objectHeld.transform.SetParent(transform);

            objectHeld.transform.localPosition = Vector3.zero;
        }
        else if(Input.GetKeyDown(KeyCode.E) && objectHeld != null)
        {
            objectHeld.transform.SetParent(null);
            objectHeld = null;
        }
    }

    void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.layer == 9 && targetObject == null) //target will be first object collided with
        {
            targetObject = collider.gameObject;
        }
    }
    void OnTriggerExit(Collider collider)
    {
        if(collider.gameObject.layer == 9)
        {
            targetObject = null;
        }
    }

You should create a new forum thread if you have a problem instead of replying to an unrelated post.