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 /
This question was closed Jan 07, 2013 at 10:41 AM by AeonIxion for the following reason:

problem solved

avatar image
0
Question by AeonIxion · Oct 17, 2012 at 12:03 PM · rigidbodythrowmouse input

throw objects based on mousemovement

Hi,

I want to throw an object into the distance when left button is released based on mousemovement (Like in Black and White: http://www.youtube.com/watch?feature=player_detailpage&v=sVSu-U1osQo#t=1009s).

I made a script (posted below) which lets you pick up the object it is attached to and throw it forward. However it is based on the mouseposition the moment you let go, I'd rather have it go further based on how fast you moved the mouse. I have no idea how to start, so if someone could point me into the right direction, that would be great!

the script:

 bool move = false;
     
     // Update is called once per frame
     void Update () 
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit, 500))
         {
             if(hit.collider.gameObject == gameObject && Input.GetMouseButton(0))
                 move = true;
     
             if(move)
             {
                 Vector3 goTo = new Vector3(hit.point.x, 2, hit.point.z);
                 gameObject.transform.position = Vector3.Lerp(transform.position, goTo, Time.deltaTime * 100);
             }
         }
         else
         {
             move = false;
         }
     }
     
     void OnMouseUp()
     {
         move = false;
         rigidbody.AddForce(Camera.main.transform.right * Input.GetAxis("Mouse X") * 15f, ForceMode.Impulse);
         rigidbody.AddForce(Camera.main.transform.up * Input.GetAxis("Mouse Y") * 20f, ForceMode.Impulse);
     }
 }



EDIT: It would seem that delta mouse position is received by Input.getAxis("Mouse X/Y"). I confused it with horizontal/verical. So I'm guessing the distance already depends on how hard you move the mouse? I'm not really happy with the result..

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

  • Sort: 
avatar image
0

Answer by Rheeenz! · Jan 04, 2013 at 03:15 PM

Hi, I'm only a beginner when it comes in programming. I want to ask if where can i put that code? you have posted above so that I can try it. Cheers! :)

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 AeonIxion · Jan 07, 2013 at 10:41 AM 0
Share

$$anonymous$$ake a script, paste the code in it and attach the script to the object you want to throw.

Closing this question now..

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Throwing a ball, target unrecognized? 0 Answers

Can I have a rigidbody continue to calculate physics while being dragged by code? 2 Answers

How to throw objects/grenades in looking height? 1 Answer

Need Help! 0 Answers

Drag and throw Rigidbody2d? 3 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