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
2
Question by Ludde · Oct 26, 2010 at 05:24 PM · pickup

Picking up/Holding objects?

Hello :)

Simular to the Half Life 2 functions, is there any easy way to make my character able to pick up objects and holding them mid-air while moving them around?

As we speak, I have some dr. pepper cans models with rigidbodys that reaaallllyyy want to be picked up and thrown around :3

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

4 Replies

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

Answer by Adam Rademacher · Oct 26, 2010 at 05:43 PM

You could try parenting them to the camera. This should make them look like they are 'floating' in the same place on the screen no matter where you look (similar to picking up a cube in portal). You'll have to reset the position every frame if you want a rigidbody on them (or set the rigidbodies to kinematic) so that they don't go flying if the player runs them into something.

Comment
Add comment · Show 3 · 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 Ludde · Oct 26, 2010 at 06:07 PM 0
Share

I see, so .. when I my "aim" my camera/center of screen against a object and press a special key (E for example), the object will be parented to the camera and since I move the camera I can move around the object..

And the object needs it's position resetted/updated every frame.. erm, well I think I understand atleast the half of it. But since this needs to be done via script, any chance have some link that I can get any smarter from? $$anonymous$$y C++ $$anonymous$$cher was not very inspiring.. :/

avatar image Ludde · Oct 26, 2010 at 06:18 PM 0
Share

I found a script that does everything for me. http://forum.unity3d.com/viewtopic.php?p=251857 Applied it to my main camera, but I seems like I can't pick up my dr peppers. Cuz "Fire1" is mouse button 1, right?

avatar image Adam Rademacher · Oct 27, 2010 at 02:09 AM 1
Share

This script has layermask detection...do you have your cans set up on a layer properly? You can also take off the layermask part of the raycast call.

The other option you can do is putting a box collider on the camera that is set to be a trigger, then use OnTriggerStay(Collider other) ins$$anonymous$$d of Update() and use that to detect other.gameObject.transform.

avatar image
2

Answer by bobin115 · Jul 17, 2014 at 07:31 AM

this is the script you will need(.js)

 var normalCollisionCount = 1;
 var spring = 50.0;
 var damper = 5.0;
 var drag = 10.0;
 var angularDrag = 5.0;
 var distance = 0.2;
 var throwForce = 500;
 var throwRange = 1000;
 var attachToCenterOfMass = false;
  
 private var springJoint : SpringJoint;
  
 function Update ()
 {
     // Make sure the user pressed the mouse down
     if (!Input.GetMouseButtonDown (0))
         return;
  
     var mainCamera = FindCamera();
  
     // We need to actually hit an object
     var hit : RaycastHit;
     if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, 100))
         return;
     // We need to hit a rigidbody that is not kinematic
     if (!hit.rigidbody || hit.rigidbody.isKinematic)
         return;
  
     if (!springJoint)
     {
         var go = new GameObject("Rigidbody dragger");
         var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
         springJoint = go.AddComponent ("SpringJoint");
         body.isKinematic = true;
     }
  
     springJoint.transform.position = hit.point;
     if (attachToCenterOfMass)
     {
         var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
         anchor = springJoint.transform.InverseTransformPoint(anchor);
         springJoint.anchor = anchor;
     }
     else
     {
         springJoint.anchor = Vector3.zero;
     }
  
     springJoint.spring = spring;
     springJoint.damper = damper;
     springJoint.maxDistance = distance;
     springJoint.connectedBody = hit.rigidbody;
  
     StartCoroutine ("DragObject", hit.distance);
 }
  
 function DragObject (distance : float)
 {
     var oldDrag = springJoint.connectedBody.drag;
     var oldAngularDrag = springJoint.connectedBody.angularDrag;
     springJoint.connectedBody.drag = drag;
     springJoint.connectedBody.angularDrag = angularDrag;
     var mainCamera = FindCamera();
     while (Input.GetMouseButton (0))
     {
         var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
         springJoint.transform.position = ray.GetPoint(distance);
         yield;
  
         if (Input.GetMouseButton (1)){
             springJoint.connectedBody.AddExplosionForce(throwForce,mainCamera.transform.position,throwRange);
             springJoint.connectedBody.drag = oldDrag;
             springJoint.connectedBody.angularDrag = oldAngularDrag;
             springJoint.connectedBody = null;
         }
     }
     if (springJoint.connectedBody)
     {
         springJoint.connectedBody.drag = oldDrag;
         springJoint.connectedBody.angularDrag = oldAngularDrag;
         springJoint.connectedBody = null;
     }
 }
  
 function FindCamera ()
 {
     if (camera)
         return camera;
     else
         return Camera.main;
 }
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
avatar image
0

Answer by MRPoof · Mar 04, 2013 at 08:27 PM

This link has a extremely similar thing to what you are asking.

http://answers.unity3d.com/questions/195921/moving-objects-in-like-in-amnesia.html

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
avatar image
0

Answer by exvalid · Oct 20, 2017 at 07:40 PM

Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

Fixed An Inversion Error and a Euler read error while moving. since yesterday it is now working as stated below

ObjectReplyIdAndLock, ObjectGrabIdAndMove

ObjectGrabIdAndLock goes on Main player,.. ObjectReplyIdAndMove goes on Moveable Objects remember to add layers in you want to hit in inspector and pick the Headcam Ect

Updated Scripts Since the other week now with full movement And Rotation and Full Inversion Options

This is Complete bar Diagnals and Mouse Rotation as im adding this now, you want to use the option OverideDiagnals and possibly UseDefaultRotation if u hate my defualt.

too add mouse copy the whole auto inversion and paste it underneath and swap the names to the mouse names instead of the default keys its a mission dont attempt it lol. i will do this over the week as still cleaing up the script its pretty large, Please contact at Exvalid@gmail.com To give me job coding.

cheers Ryan kappeslink text Exvalid@gmail.comlink text


objectgrabscripts.zip (11.7 kB)
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Picking up Objects (from within the player's code) 1 Answer

What's the best way to do this? 1 Answer

Buff System 1 Answer

How do I edit springjoint components in script? 1 Answer

Picking up objects and dropping them 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