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
0
Question by Aktarus_bham · Feb 24, 2014 at 01:32 AM · dragdropgrabthrowhold

Grab and throw object immediately

Hello everyone,

I would like that the player could grab an object and throw it using some force depending on the speed of the mouse and the angle/direction, I am using the script "DragRigidbody.js", it works well but there is an issue: The script makes sure to grab an object and can hold as long as the player would until it releases the right mouse button. What I would like is to only allow the player to catch an object and throw it immediately, for example grab it for 0.3 seconds, otherwise the object falls from his hands. Any help, please?

Thanks

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

1 Reply

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

Answer by robertbu · Feb 24, 2014 at 01:39 AM

You are going to need to modify the DragRigidbody.js script. Personally I'd make a new .js file...like MyDragRigidbody.js....and copy the contents to the file. I don't like multiple, different versions of standard scripts hanging around.

To make the changes you require, towards the top of the file (like 7 on my copy), put

 private timestamp = 0.0;

Then on line 25 put:

 timestamp = Time.time;


Finally modify line 61. Currently it will read:

 while (Input.GetMouseButton (0))

And you want change it to:

  while ((timestamp + 0.3 >= Time.time) && Input.GetMouseButton (0))

The 0.3 is the hold time you specified. The new condition to the while() loop compares the timestamp when the mouse button went down to the current time. If the current time is more than 0.3 ahead, it terminates the while() loop. This while() loop is doing the dragging.

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 Aktarus_bham · Feb 24, 2014 at 05:26 PM 0
Share

Thanks robertu! However it doesn't works, I don't know why. It seems that the code timestamp = Time.time; is not at the right place. Could you show me your complete code please?

avatar image robertbu · Feb 24, 2014 at 05:35 PM 0
Share

I had the condition backwards in the last line. I fixed my answer. Here is an alternate that might be more understandable:

 while (((Time.time - timestamp) < 0.3) && Input.Get$$anonymous$$ouseButton (0))
avatar image Aktarus_bham · Feb 24, 2014 at 05:52 PM 0
Share

Very Nice!!! It Works! Thanks robertu.

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Throwing an object (applying force to it when mouse is released) 2 Answers

Unity 3d hold/grab/lift/throw/choke? 0 Answers

When is the OnDrop method called? IDragHandler 1 Answer

Drag n Drop in a 3D orthographic environment 1 Answer

Drag and Drop Using Mouse (2D) 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