- Home /
Pick up and drop rigidbody
I'm having a slight problem making a custom pickup/drop script.
Essentially what I'm after is allowing the player to left-click an object and have it parented to the player, move around the scene and drop the object with another left-click.
I have many different objects the player can pick up, varying in shape and size. A problem I encountered was that the object didn't always sit perfectly to allow me to click ON it to release. To counter this I've implemented a system where the player clicks ON an object to pick it up, but can then click anywhere to release it.
The new problem I've encountered and have so far been unable to fix is that the left-click in the Update function is set off at the same time as the OnMouseDown function and the player picks up and drops the object at the same time.
Any ideas how I can get around this?
var pickupDistance = 3.0; static var heldObject : GameObject; static var itemHeld = false; var objectDist = -.1; private var hand :GameObject;
function Start() { hand = gameObject.FindWithTag("Player"); }
function Update () { if (Input.GetMouseButtonUp(0) && itemHeld == true) { hand.animation.Play("drop"); heldObject.transform.parent = null; heldObject.GetComponent(Rigidbody).isKinematic = false; heldObject = null; itemHeld = false; } }
function OnMouseDown() { if(itemHeld == false) { heldObject = gameObject; hand.animation.Play("grab"); heldObject.transform.parent = gameObject.FindWithTag("Player").transform; heldObject.GetComponent(Rigidbody).isKinematic = true; var handLocation = Vector3(0,objectDist,0); heldObject.transform.localPosition = handLocation; itemHeld = true; } }
Answer by IJM · Sep 19, 2010 at 04:13 AM
This is simple. Try this:
var pickupDistance = 3.0; static var heldObject : GameObject; static var itemHeld = 0; var objectDist = -.1; private var hand :GameObject;
function Start() { hand = gameObject.FindWithTag("Player"); }
function Update () { if (Input.GetMouseButtonDown(0) && itemHeld == 2) { hand.animation.Play("drop"); heldObject.transform.parent = null; heldObject.GetComponent(Rigidbody).isKinematic = false; heldObject = null; itemHeld = 0; } }
function OnMouseDown() { if(itemHeld == 0) { heldObject = gameObject; hand.animation.Play("grab"); heldObject.transform.parent = gameObject.FindWithTag("Player").transform; heldObject.GetComponent(Rigidbody).isKinematic = true; var handLocation = Vector3(0,objectDist,0); heldObject.transform.localPosition = handLocation; itemHeld = 1; } }
function OnMouseUp(){ if(itemHeld == 1) { itemHeld = 2; } }
Hey mate, thanks for the response, but that's essentially the same logic, just offset slightly with the extra On$$anonymous$$ouseUp function. As far as I can tell it does the same thing as the code I've got.
Ah, actually cancel that. If you change the "Input.Get$$anonymous$$ouseButtonUp" to an "Input.Get$$anonymous$$ouseButtonDown" within the update function it works.
Thanks ;)
This script works, thanks! but can i somehow change it so that i can pick the obejct up with my crosshair or the center of the screen? Would be realy helpfull!
Screen.lockCursor. I don't know if that's the right spelling, but look it up. It centers the mouse, makes it invisible and holds it hostage until you press escape or leave.
Answer by ShadyGamer · Feb 26, 2014 at 11:12 AM
Hey could you possibly translate it to C#?
float pickupDistance = 3.0f;
static Gameobject heldObject;
static int itemHeld = 0;
float objectDist = -.1;
private GameObject hand;
Start() {
hand = gameObject.FindWithTag("Player");
}
Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0) && itemHeld == 2) {
hand.animation.Play("drop");
heldObject.transform.parent = null;
heldObject.GetComponent(Rigidbody).is$$anonymous$$inematic = false;
heldObject = null;
itemHeld = 0;
}
}
On$$anonymous$$ouseDown() {
if(itemHeld == 0) {
heldObject = gameObject;
hand.animation.Play("grab");
heldObject.transform.parent = gameObject.FindWithTag("Player").transform;
heldObject.GetComponent(Rigidbody).is$$anonymous$$inematic = true;
var handLocation = Vector3(0,objectDist,0);
heldObject.transform.localPosition = handLocation;
itemHeld = 1;
}
}
On$$anonymous$$ouseUp(){
if(itemHeld == 1)
{
itemHeld = 2;
}
}
That should do it for you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073449im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Duplicated object doesn't collide but original does 1 Answer
Pick up a rigidbody and have it remain still? (java) 0 Answers
Picking up rigidbody objects 2 Answers
drag rigidbodies script 1 Answer
Picking / Dragging Objects with Mouse 6 Answers