• 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
0
Question by MrSteve1 · Oct 11, 2012 at 11:38 PM · rigidbodydragonmousedown

Click on Object

Hi All,

This is a bit of an obscure question so I hope it makes sense.

Basically, I have these objects in my scene that add a DragRigidbody component when you click the mouse down and Destroys it when you release with this simple code.

 function OnMouseDown(){
     gameObject.Find("Plank(Clone)").AddComponent(DragRigidbody);
 }
 function OnMouseUp(){
     Destroy(gameObject.Find("Plank(Clone)").GetComponent(DragRigidbody));
 }

Now I want somthing to happen when you only click and release (NO DRAGGING INVOLVED). Does anyone know how I might achieve this without the GUITextures appearing on the first click (I.e when you drag the item)

Hope this all makes sense!

Comment
Add comment · Show 1
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 hvilela · Oct 12, 2012 at 12:16 AM 1
Share

I don't get the relationship with the GUITextures.

2 Replies

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

Answer by kmeboe · Oct 12, 2012 at 12:52 AM

I'm not sure what you mean about GUITextures. But the following code should prevent you from taking action if the mouse is dragged during the click:

 var dragged : boolean = false;
 
 function OnMouseDown(){
     dragged = false;
     gameObject.Find("Plank(Clone)").AddComponent(DragRigidbody);
 }
 function OnMouseDrag(){
     dragged = true;
 }
 function OnMouseUp(){
     Destroy(gameObject.Find("Plank(Clone)").GetComponent(DragRigidbody));
     
     if (!dragged)
     {
         // Extra functionality -- only if not dragged.
        }
 }
Comment
Add comment · Show 3 · 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 MrSteve1 · Oct 12, 2012 at 01:42 PM 0
Share

Sorry I should have specified, I want some GUITextures to enable when I click the object, but nothing happen when I drag the object. Thanks for you answer. I didn't know there was such a function. I will try that.

avatar image MrSteve1 · Oct 12, 2012 at 02:25 PM 0
Share

That worked thank you! I had to add a simple check for the rigidbody velocity due to the complexity of the collider. But it finally works. Thank you so much!

avatar image kmeboe · Oct 12, 2012 at 04:26 PM 0
Share

You're welcome! Glad I could help.

avatar image
0

Answer by sparkzbarca · Oct 12, 2012 at 12:52 AM

ack published but misunderstood

the easiest would be to simply start a timer and only add the drag aspect after so many seconds alternatly if you attempt to move the cursor

for example old_cursor_position

 if(input.getaxis('mouse X') != 0 && input.getaxis('mouse Y') != 0) // mouse moving
 {
     add drag component
 }

 alternatly 
 onmousedown set timer
 onmouseup stop timing, reset to zero
 
 if(timer == its been too long, you've held down for 1 full second)
 {
 add drag component
 }

helps?

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Stop a Game Object from Passing through a Collider while it's being Dragged? 3 Answers

Drag Rigidbody 2 Answers

Rigidbody and raycasting 6 Answers

Moving a rigidbody onto exact mouse position using rigidbody.MovePosition? 1 Answer

What would be a good physics model model for 2d ship? 1 Answer


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