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 DXOFTX · Jan 14, 2014 at 11:55 PM · physicsobjectsclickpickuplift

How do you pick up an object with a tag? [must have physics and no inventory]

So I'm making a game which has an amnesia style system of object interactivity including cabinets / drawers opening. However after about 6 hours of searching I couldn't find a single good code that worked fully. Also i would like the object to rotate via mouse.

[ex. Garry's mod holding E with something in your physgun makes it rotate]

currently i have a crosshair script which has a function for "firing" from the midpoint of the screen.

Please help. I'm very new to coding in unity and extremely new to Javascript / C#

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

3 Replies

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

Answer by Josh707 · Jan 16, 2014 at 12:23 AM

Ok, I made an Amnesia clone a while back using coroutines like this for rigidbody interaction so this should behave how you wanted, it pulls objects from their center rather than a single point like with the spring joint method. It just needs an empty parented to the camera.

 public Transform player_camera;
 public Transform phys_dragger; //empty parented to camera
 public float grab_distance = 5.0f;
 private float temp_drag = 10.0f;

 IEnumerator DragObject(Rigidbody r){
     float oldDrag = r.drag;
     r.drag = temp_drag;
     r.useGravity = false;
     while(Input.GetButton("Interact")){
         //Keep aligned with camera's view
         r.transform.rotation = Quaternion.Slerp(r.transform.rotation, phys_dragger.rotation, Time.deltaTime * 15.0f);
         //Vector that points from rigidbody to grab point
         Vector3 vel = (phys_dragger.position - r.position).normalized;
         //Clamp distance to 1, otherwise it could be pulled through walls
         float d = Mathf.Min(Vector3.Distance(r.transform.position, phys_dragger.position), 1.0f);
         r.AddForce(vel * d, ForceMode.VelocityChange);
         
         if(Input.GetButton("Throw")){
             r.rigidbody.AddForce(player_camera.transform.forward * 5, ForceMode.Impulse);
             r.drag = oldDrag;
             r.useGravity = true;
             yield break;
         }
         yield return new WaitForFixedUpdate();
     }
     r.drag = oldDrag;
     r.useGravity = true;
 }

Then it would be used with a raycast so you can pass it the rigidbody that was hit without needing a permanent variable to store it in.

 void Update () {
     if(Input.GetButtonDown("Interact")){
         RaycastHit hit;
         if(Physics.Raycast(player_camera.position, player_camera.forward, out hit, grab_distance)){
             if(hit.rigidbody){
                 phys_dragger.position = hit.rigidbody.position;
                 phys_dragger.rotation = hit.rigidbody.rotation;
                 StartCoroutine(DragObject(hit.rigidbody));
             }
         }
     }
 }


You'll want to mess around with the temporary drag and forces, if you lower the drag you'll notice it will orbit around the point but if it's high the object will move too slow.

Comment
Add comment · Show 2 · 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 DXOFTX · Jan 16, 2014 at 01:20 AM 0
Share

I tried Adding the code twice both in js and cs however on js i get too many errors and on cs i get 2 which i don't understand at all. =\

alt text

ss (2014-01-15 at 05.20.16).png (6.8 kB)
avatar image Josh707 · Jan 16, 2014 at 05:27 AM 0
Share

Hmm, I wrote that in C# so it won't work if you just paste it into a JS file. IEnumerator would be replaced with function and so on. $$anonymous$$ake sure you have the class name and using namespace directives at the top:

 using UnityEngine;
 using System.Collections;
 
 public class grabber : $$anonymous$$onoBehaviour {

If that's not the problem I'm not sure what is, what version are you using? Here's a copy/paste of the working script in case I screwed something up

http://pastebin.com/04qTsJiP

avatar image
0

Answer by robertbu · Jan 15, 2014 at 12:01 AM

Having never played it, I watched a short clip of the Video Game. For some objects (like dragging a table around), it looked like what would happen if you used the standard DragRigidbody.js script. You can get it:

 Assets > Import Package > Scripts

Other appeared that they may not be using the physics (like opening a door). Note in a 3D game sometimes mapping the 2D mouse into the scene can be difficult. I have a bit of started code for someone who wanted to open a door with dragging here:

http://answers.unity3d.com/questions/614173/drag-door-where-to-start.html

Comment
Add comment · Show 7 · 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 DXOFTX · Jan 15, 2014 at 12:36 AM 0
Share

Thanks for the door tutorial, well i suppose that leaves me with only the item lifting and moving left

avatar image robertbu · Jan 15, 2014 at 01:34 AM 0
Share

Did you try the DragRigidbody.js script? This allows arbitrary dragging of objects.

avatar image DXOFTX · Jan 15, 2014 at 05:59 AM 0
Share

That helps quite a bit however i don't need it attached to the mouse but the center cross hair and i have no idea how to manipulate that, the view is first person

avatar image Josh707 · Jan 15, 2014 at 06:22 AM 0
Share

You can save a vector3 of the objects position when you first grab it, the easiest would be moving an empty gameobject which is a child of the camera so it rotates with your view, and calculate a vector from the object to the initial point scaled by the distance and use AddForce to push it there. If you look at the coroutine used in this script:

http://wiki.unity3d.com/index.php/Drag%26Throw

You can modify it to do what I described above, it works best if you temporarily increase the drag values or the rigidbody will move too fast and orbit around the point

avatar image DXOFTX · Jan 15, 2014 at 06:31 AM 0
Share

It works for the most part but there's one problem [shown in image]

alt text

it's being held by the mouse which is the problem

ss (2014-01-14 at 10.29.04).png (413.8 kB)
Show more comments
avatar image
0

Answer by DXOFTX · Jan 16, 2014 at 07:59 AM

Thanks dude! It took a bit of futzing about but i got it to work. oddly enough the paste bin version worked and the code in here glitched out the editor for a bit. however just with a bit of messing about and minor code patching i got it to work 100%

Thanks again for both awesome answers :D

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 Josh707 · Jan 18, 2014 at 07:03 PM 0
Share

Hehe that's weird, glad you got it figured out though!

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

Determining location of intersection from position, rotation, rotation speed, and forward speed of objects. 0 Answers

Rigidbody item pickup problem 0 Answers

Object carry in game 1 Answer

Change Pickup key 1 Answer

lifting physic objects with dynamic weight objects on it 2 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