- Home /
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.
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
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!
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:
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 yourUpdate()
function), let's call itgrabbed
(so somewhere outside ofUpdate()
you would have something likevar 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; }
Now that you know which object you're grabbing, you can manipulate its transform as you please, still in the
Update()
function after step 1if(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; }
Your answer
Follow this Question
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