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 slyvvr · Jan 16, 2013 at 11:31 AM · javascriptmousedragdropthrow

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

I would like to be able to pick up, drag along, and finally throw an object.

this is my code so far: what it does so far is pick up the GameObject (the one i used in my tests is a cube named DraggableBox, located above a plane, with the Tag "draggable") drag it along my mouse, and then drop it again once the mouse is released. Now what i am trying to accomplish now (and so far haven't succeeded in) is to throw the object if the mouse is moving at the moment the mouse button is released, instead of just drop it. Much like the game Black and White 2.

Thanks in advance for any help you can give me with this.

 var grabbed : Transform;
 var grabDistance : float = 10.0f;
 var moved : boolean = false;
 var DraggableObject : GameObject;
 var goTo : Vector3;
 var targetPosition:Vector3;
 var startPosition:Vector3;
 var direction: Vector3;
 function Update () {
     if (Input.GetMouseButton(0)) {
         if (grabbed){
             Drag();
         }
         else{ 
               Grab();
           }
       }
     else{
         grabbed = null;
     }    
     if(Input.GetMouseButtonUp(0) && grabbed != null){
         Throw();
     }       
 }
 
 
 function Grab() {
     if (grabbed){ 
        grabbed = null;
     }
     else {
         var hit : RaycastHit;            
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, hit) && hit.collider.gameObject == GameObject.FindGameObjectWithTag("Draggable")){
             startPosition= Input.mousePosition;
             grabbed = hit.transform;
             moved = true;
             DraggableObject = hit.collider.gameObject;
             
         } 
             
     }
 }
 function Throw() {
     if (grabbed && moved==true){
         targetPosition=Input.mousePosition;
         direction = (targetPosition - startPosition);
         direction.Normalize();
          
        rigidbody.AddForce(direction * 15f, ForceMode.Impulse);
        moved = false;
         
     }    
 }
 
 function Drag() {
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     grabbed.position = ray.origin + ray.direction * grabDistance;
 }
 
Comment
Add comment · Show 3
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 danilonishimura · Jan 16, 2013 at 03:24 PM 0
Share

If you're trying to create something like the gravity gun from Half Life, here's and idea of how i'd try to implement:

  • Cast a ray from the camera to the mouse position

  • Create an empty GameObject (lets call it destination) in the collision point

  • $$anonymous$$ove the destination GameObject to the camera's hierarchy

  • Add into the collided object a script that will, essencially, apply a force (relative to distance) towards the destination GameObject's direction

  • When the user clicks, remove that script and aplly a force to the desired direction (away from the camera?)

avatar image slyvvr danilonishimura · Jan 16, 2013 at 05:35 PM 0
Share

that's not what i am trying to create here lol, did you read what i typed? i said like black and white, meaning you pick up and object, lets say you hold it in your hand for the time being. and then you move your mouse, let go off the button and then it flies away, as if you would have thrown it. not a gravity gun at all

avatar image liveuk · Oct 30, 2015 at 09:59 AM 0
Share

How far did you get with this? I am also making a Black & White style game

$$anonymous$$ike

1 Reply

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

Answer by robertbu · Jan 16, 2013 at 02:33 PM

The DragRigidbody.js comes with Unity (Assets/Import Package/Scripts) and implements this behavior. If I remember correctly they do this with a hinge joint, making the connection on mouse down and releasing it on up.

Comment
Add comment · Show 4 · 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 robertbu · Jan 16, 2013 at 02:45 PM 0
Share

Your Throw() routine will never be called because grabbed gets set to null before you call Get$$anonymous$$ouseButtonUp(0). You need to restructure your logic a bit in Update().

avatar image slyvvr · Jan 16, 2013 at 02:51 PM 0
Share

Thanks, i didn't realize that >.<

avatar image slyvvr · Jan 16, 2013 at 03:04 PM 0
Share

i fixed it, now it gets called, but it's, still not throwing

avatar image robertbu · Jan 16, 2013 at 04:07 PM 0
Share

Do you have a Rigidbody attached (probably want gravity turned off)? I hacked out the grabbed = null line, and it works for me (with some issues).

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

11 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

Related Questions

Orbital cameras and dragging objects with mouse. 0 Answers

Drag and Drop Using Mouse (2D) 1 Answer

Grab and throw object immediately 1 Answer

Drag Object along local z-axis. Using WorldToScreenPoint. 1 Answer

trying to move an object with the mouse 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