• 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 artsdcs · Dec 16, 2011 at 09:01 PM · rigidbodydragmovingdraggingthrowing

moving objects in like in amnesia

i always found it realy enjoyable in amnesia you could pick things up and move them around ans stuff. i would like that to. and since i'm very new an still experimenting and stuff i would like to know if there ia any script where i could just simply pick up an object and throw it no rotating. i already tried the dragrigidbody script you get with unity but you need to touch the rigid body from very close and i dont want tha. i want to be able to pick it up from a distance. i searched long on the internet and haven't found anything i could use so please could somebody help me with it i'm scripting everything in javascript. i already have a mouse lock and a crosshair the controls i would like are:gold mouse0 to lift it and press mouse1 to throw it. i hope this is enough information and if someone know if there is already anscript like i'm describing please point me to it.

sry for any spelling mistakes i'm dutch.

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

8 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by exvalid · Oct 20, 2017 at 07:43 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

ObjectGrabIdAndLock, ObjectReplyIdAndMove

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

Answer by Mayday556 · Aug 03, 2013 at 04:57 PM

I made this myself, tested, 100% works. Promise. I haven't made the throwing work yet, but I'm working on that now.

 using UnityEngine;
 using System.Collections;
 
 public class HoldObject : MonoBehaviour {
 
     float drag = 1.0f;
     float angularDrag = 5.0f;
     bool attachToCenterOfMass = false;
     
     private FixedJoint fixedJoint;
     
     void Update() {
         
         // Make sure the user pressed the mouse down
         if (!Input.GetMouseButtonDown (0))
             return;
     
         var mainCamera = FindCamera();
             
         // We need to actually hit an object
         RaycastHit hit;
         if (!Physics.Raycast(mainCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2)), out hit, 100))
             return;
         
         // We need to hit a rigidbody that is not kinematic
         if (!hit.rigidbody || hit.rigidbody.isKinematic)
             return;
         
         if (!fixedJoint) {
             
             GameObject go = new GameObject("Rigidbody dragger");
             Rigidbody body = go.AddComponent("Rigidbody") as Rigidbody;
             fixedJoint = go.AddComponent("FixedJoint") as FixedJoint;
             body.isKinematic = true;
         }
         
         fixedJoint.transform.position = hit.point;
         if (attachToCenterOfMass) {
             
             Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
             anchor = fixedJoint.transform.InverseTransformPoint(anchor);
             fixedJoint.anchor = anchor;
         } else {
             fixedJoint.anchor = Vector3.zero;
         }
         
         fixedJoint.connectedBody = hit.rigidbody;
         
         StartCoroutine("DragObject", hit.distance);
     }
     
     IEnumerator DragObject (float distance) {
         
         float oldDrag = fixedJoint.connectedBody.drag;
         float oldAngularDrag = fixedJoint.connectedBody.angularDrag;
         
         fixedJoint.connectedBody.drag = drag;
         fixedJoint.connectedBody.angularDrag = angularDrag;
         
         Camera mainCamera = FindCamera();
         
         while (Input.GetMouseButton (0)) {
             
             var ray = mainCamera.ScreenPointToRay (new Vector2(Screen.width / 2, Screen.height / 2));
             fixedJoint.transform.position = ray.GetPoint(distance);
             yield return null;
         }
         
         if (fixedJoint.connectedBody) {
             
             fixedJoint.connectedBody.drag = oldDrag;
             fixedJoint.connectedBody.angularDrag = oldAngularDrag;
             fixedJoint.connectedBody = null;
         }
     }
     
     Camera 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 Doireth · Jul 19, 2012 at 10:21 PM

I posted a script at

http://answers.unity3d.com/questions/287063/dragging-objects-like-in-amnesia.html#answer-287105

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 psychentist · Apr 30, 2012 at 02:08 AM

I wanted to do something similar, but FAIR WARNING, Even if you get this script working, It won't act like amnesia. Unity STILL has issues getting objects to obey scripts and physics at the same time. Either you will have a rigidbody on the object, which will cause it to fall unless you are actively moving it upwards, or you will have gravity off, which will cause it to fly into space when the object touches the ground, or you will not have a rigidbody, and the object will not obey normal physics, but will be a slave to the script, allowing it to pass through the floor and such. If you really want, I could upload the script I used, but IDK a workaround for the issues involved.

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 exvalid · Oct 20, 2017 at 07:46 PM 0
Share

$$anonymous$$ove the object from the object should solve this problem.

avatar image exvalid · Oct 20, 2017 at 07:47 PM 0
Share

i used 2 scripts to do it and a very selective call prossess.

avatar image
0

Answer by BarkShark · Dec 18, 2011 at 06:44 PM

Hmm, you still make a couple of basic mistakes, an if statement has to go in '()'. For example :

 if(condition is true)
 {
 do something;
 }


This is the full code to do what you want:

 var hitObject : GameObject;
 
 function Update()
 {
     if(Input.GetMouseButtonDown(0))  
     {
     
     var hit : RaycastHit;
     
     // Cast a ray
     
     if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
     {
     
     if(hitObject.collider.tag == "Pick")
     {
     
     hitObject = hit.collider.gameObject;
     hitObject.transform.parent = gameObject.transform;
     }
     }
     }
     }
     
     if(Input.GetMouseButtonUp(0)&& hitObject != null) // This will release the object 
     {
     hitObject.transform.parent = null;
     hitObject = null;
     }
     }
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 artsdcs · Dec 18, 2011 at 08:43 PM 0
Share

got an error becuse of the last } so i removed it and i got to play but i could not pick op the cube with the Pick tag so i added the cube to the hit object and now i can still pickup everything like the wall and ground and i can't release it. and also if it finally works so i can only pick up objects that are like 5 meter away from the middle from the camera will that to go trough the wall to?

thank you so far

  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Drag Rigidbody that respects collisions. 1 Answer

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

Drag rigidbody.js help 0 Answers

Enemies are moving aroung wildly 1 Answer

How to influence the direction of an falling object by dragging? 1 Answer


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