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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by FatalNightmare · Nov 09, 2013 at 07:58 PM · rigidbodyrotatedragzoomdrop

Pick up, drop, rotate, and zoom a rigidbody gameObject

Hey guys, so I have been stuck on this problem for a while now. I have a still camera looking down on a map (Birds eye view / top-down) and there are object in this map (chair, table, desk) that are rigidbodies. I would like it so that when you click on one of these objects they get picked up (or stuck) to the mouse and can be moved around the room. This would be great but if you could I would also like it so that using the mouse wheel the object moves up or down. Then using 'r' or any button you can rotate that object on its 'y' axis, and every time 'r' is pressed it is rotated 90 degrees. Sorry that I don't have code, I got frustrated and deleted it all. Any help is appreciated, sorry if it is confusing or long. Have a good day.

NOTE: I mainly work in Javascript so that would be recommended, but if you know how to do it in another language I would enjoy that as well.

Comment
Add comment · Show 2
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 · Nov 10, 2013 at 12:07 AM 0
Share

I did a rewrite for another question to add scroll wheel functionality to the standard DragRigidbody script:

http://answers.unity3d.com/questions/556323/dragrigidbody-scroll-in-and-out.html

avatar image 3mjay · Mar 13, 2016 at 09:39 PM 0
Share

why dont you guys just write the code from start to finish ?! Like this it doesn't work without us super beginner guys spending 5 days trying to troubleshoot the codes!

I love frustrations feelings!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Wiki

Answer by TranquilMarmot · Nov 10, 2013 at 02:14 AM

Sorry to hear you're frustrated. That's a fact of life in game development! Shrug it off, learn from your mistakes and move on!

I don't want to write the entire script for you, but I can describe the steps and give you some code that will hopefully point you in the right direction :)

Please note I usually use C# so my JS might not be 100% on-point. Anybody, please correct any mistakes (community edit is on). I also didn't test all of this, so it might not be totally correct.

In your script's Update() function:

  1. If the mouse is down and you don't have anything selected, perform a ray cast from where the camera is to where the mouse click is and if there's a hit, save it to some var you have in your script's global scope (can be seen/accessed outside of your Update() function), let's call it grabbed (so somewhere outside of Update() you would have something like var grabbed : GameObject).

      if(Input.GetMouseButton(0) && !grabbed){
             // grab any raycast hit
             var hit : RaycastHit;
             var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, hit)){
                 // You might want to do some sort of filtering here so you don't grab things you don't want to, such as the ground (a different topic altogether)
                 grabbed = hit.collider.gameObject;
             } else {
                 grabbed = null;
             }
         } else {
             // set grabbed to null so we know we're not holding onto anything
             grabbed = null;
         }
    
    
  2. Now that you know which object you're grabbing, you can manipulate its transform as you please, still in the Update() function after step 1

      if(grabbed){
              // my guess is that you're also going to want to stop the object from moving/rotating on its own while it's grabbed
              grabbed.rigidbody.velocity = Vector3.zero;
              grabbed.rigidbody.angularVelocity = Vector3.zero;
         
         
             /* rotate by 90 degrees */
             if(Input.GetKeyDown(KeyCode.R)){
                 var euler = grabbed.transform.rotation.eulerAngles;
                 euler.y += 90.0f;
                 grabbed.transform.rotation = Quaternion.Euler(euler);
             }
         
         
              /* move by mouse movement */
              // NOTE: These have to be defined in Edit -> Project Settings -> Input as an axes, should be there by default.
              float scroll = Input.GetAxis("Mouse ScrollWheel");
              float mouseDX = Input.GetAxis("Mouse X");
              float mouseDY = Input.GetAxis("Mouse Y");
             
              // NOTE: This will probably move the object by WAY too much.
              // You will need to make these numbers smaller
              // like, say, divide them by 2 or something; test to figure out what works best for your scenario
              // If it moves on the wrong axes, try mixing these around or negating them.
              var trans = Vector3(mouseDX, scroll, mouseDY);
    
              // Rotate translation to correlate to camera
              // Not sure if these next two lines work in JS, let me know
              trans = Camera.main.transform.rotation * trans;
              grabbed.transform.position += trans;
          }
    
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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Drag and drop ray-casting conundrum! 1 Answer

Rotate on drag for IOS? 1 Answer

How to rotate an object based on angles? 1 Answer

Drag Drop Inventory With GUI Buttons. 0 Answers

adding drag to a relative force 0 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