Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Arch · Sep 19, 2010 at 01:34 AM · rigidbodypickupdrag-and-dropgetmousebuttondrag-rigidbody

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; } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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; } }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arch · Sep 19, 2010 at 08:18 AM 0
Share

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.

avatar image Arch · Sep 19, 2010 at 08:24 AM 1
Share

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 ;)

avatar image IJM · Sep 19, 2010 at 07:41 PM 0
Share

Sry, I forgot to change that. =)

avatar image martokio97 · Feb 08, 2012 at 04:15 PM 1
Share

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!

avatar image JiffyJuff · Jul 02, 2013 at 12:29 PM 0
Share

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.

avatar image
1

Answer by ShadyGamer · Feb 26, 2014 at 11:12 AM

Hey could you possibly translate it to C#?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dragonfly3r · Sep 25, 2016 at 09:37 PM 0
Share
 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges